Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I...
Transcript of Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I...
![Page 1: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/1.jpg)
Code Generation: Sethi Ullman Algorithm
Amey Karkare
March 28, 2019
![Page 2: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/2.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).
I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 3: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/3.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 4: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/4.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 5: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/5.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 6: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/6.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 7: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/7.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 8: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/8.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 9: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/9.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 10: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/10.jpg)
Sethi-Ullman Algorithm – Introduction
I Generates code for expression trees (not dags).I Target machine model is simple. Has
I a load instruction,
I a store instruction, and
I binary operations involving either a register and a memory, or
two registers.
I Does not use algebraic properties of operators. If a ∗ b has to
be evaluated using r1 ← r1 ∗ r2, then a and b have to be
necessarily loaded in r1 and r2 respectively.
I Extensions to take into account algebraic properties of
operators.
I Generates optimal code – i.e. code with least number of
instructions. There may be other notions of optimality.
I Complexity is linear in the size of the expression tree.
Reasonably efficient.
![Page 11: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/11.jpg)
Expression Trees
I Here is the expression a/(b + c)− c ∗ (d + e) represented as a
tree:
/ *
+ +a
b c
c
d e
_
![Page 12: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/12.jpg)
Expression Trees
I Here is the expression a/(b + c)− c ∗ (d + e) represented as a
tree:
/ *
+ +a
b c
c
d e
_
![Page 13: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/13.jpg)
Expression Trees
I Here is the expression a/(b + c)− c ∗ (d + e) represented as a
tree:
/ *
+ +a
b c
c
d e
_
![Page 14: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/14.jpg)
Expression Trees
I We have not identified common sub-expressions; else we
would have a directed acyclic graph (DAG):
/ *
+ +a
b c d e
_
![Page 15: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/15.jpg)
Expression Trees
I Let Σ be a countable set of variable names, and Θ be a finite
set of binary operators. Then,
1. A single vertex labeled by a name from Σ is an expression tree.
2. If T1 and T2 are expression trees and θ is a operator in Θ,
thenT T
1 2
θ
is an expression tree.
I In this example
Σ = {a, b, c , d , e, . . . }, and Θ = {+, −, ∗, /, . . . }
![Page 16: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/16.jpg)
Expression Trees
I Let Σ be a countable set of variable names, and Θ be a finite
set of binary operators. Then,
1. A single vertex labeled by a name from Σ is an expression tree.
2. If T1 and T2 are expression trees and θ is a operator in Θ,
thenT T
1 2
θ
is an expression tree.
I In this example
Σ = {a, b, c , d , e, . . . }, and Θ = {+, −, ∗, /, . . . }
![Page 17: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/17.jpg)
Expression Trees
I Let Σ be a countable set of variable names, and Θ be a finite
set of binary operators. Then,
1. A single vertex labeled by a name from Σ is an expression tree.
2. If T1 and T2 are expression trees and θ is a operator in Θ,
thenT T
1 2
θ
is an expression tree.
I In this example
Σ = {a, b, c , d , e, . . . }, and Θ = {+, −, ∗, /, . . . }
![Page 18: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/18.jpg)
Expression Trees
I Let Σ be a countable set of variable names, and Θ be a finite
set of binary operators. Then,
1. A single vertex labeled by a name from Σ is an expression tree.
2. If T1 and T2 are expression trees and θ is a operator in Θ,
thenT T
1 2
θ
is an expression tree.
I In this example
Σ = {a, b, c , d , e, . . . }, and Θ = {+, −, ∗, /, . . . }
![Page 19: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/19.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 20: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/20.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 21: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/21.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 22: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/22.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 23: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/23.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 24: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/24.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 25: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/25.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 26: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/26.jpg)
Target Machine Model
I We assume a machine with finite set of registers r0, r1, . . ., rk ,
countable set of memory locations, and instructions of the
form:
1. m← r (store instruction)
2. r ← m (load instruction)
3. r ← r op m (the result of r op m is stored in r)
4. r2 ← r2 op r1 (the result of r2 op r1 is stored in r2)
I Note:
1. In instruction 3, the memory location is the right operand.
2. In instruction 4, the destination register is the same as the left
operand register.
![Page 27: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/27.jpg)
Key Idea
I Determines an evaluation order of the subtrees which requires
minimum number of registers.
I If the left and right subtrees require l1, and l2 (l1 < l2)
registers respectively, what should be the order of evaluation?
opl2
l1
![Page 28: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/28.jpg)
Key Idea
I Determines an evaluation order of the subtrees which requires
minimum number of registers.
I If the left and right subtrees require l1, and l2 (l1 < l2)
registers respectively, what should be the order of evaluation?
opl2
l1
![Page 29: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/29.jpg)
Key Idea
I Choice 1
1. Evaluate left subtree first, leaving result in a register. This
requires upto l1 registers.
2. Evaluate the right subtree. During this we might require upto
l2 + 1 registers (l2 registers for evaluating the right subtree and
one register to hold the value of the left subtree.)
I The maximum register requirement in this case is
max(l1, l2 + 1) = l2 + 1.
![Page 30: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/30.jpg)
Key Idea
I Choice 1
1. Evaluate left subtree first, leaving result in a register. This
requires upto l1 registers.
2. Evaluate the right subtree. During this we might require upto
l2 + 1 registers (l2 registers for evaluating the right subtree and
one register to hold the value of the left subtree.)
I The maximum register requirement in this case is
max(l1, l2 + 1) = l2 + 1.
![Page 31: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/31.jpg)
Key Idea
I Choice 1
1. Evaluate left subtree first, leaving result in a register. This
requires upto l1 registers.
2. Evaluate the right subtree. During this we might require upto
l2 + 1 registers (l2 registers for evaluating the right subtree and
one register to hold the value of the left subtree.)
I The maximum register requirement in this case is
max(l1, l2 + 1) = l2 + 1.
![Page 32: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/32.jpg)
Key Idea
I Choice 1
1. Evaluate left subtree first, leaving result in a register. This
requires upto l1 registers.
2. Evaluate the right subtree. During this we might require upto
l2 + 1 registers (l2 registers for evaluating the right subtree and
one register to hold the value of the left subtree.)
I The maximum register requirement in this case is
max(l1, l2 + 1) = l2 + 1.
![Page 33: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/33.jpg)
Key Idea
I Choice 2
1. Evaluate the right subtree first, leaving the result in a register.
During this evaluation we shall require upto l2 registers.
2. Evaluate the left subtree. During this, we might require upto
l1 + 1 registers.
I The maximum register requirement over the whole tree is
max(l1 + 1, l2) = l2
Therefore the subtree requiring more registers should be
evaluated first.
![Page 34: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/34.jpg)
Key Idea
I Choice 2
1. Evaluate the right subtree first, leaving the result in a register.
During this evaluation we shall require upto l2 registers.
2. Evaluate the left subtree. During this, we might require upto
l1 + 1 registers.
I The maximum register requirement over the whole tree is
max(l1 + 1, l2) = l2
Therefore the subtree requiring more registers should be
evaluated first.
![Page 35: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/35.jpg)
Key Idea
I Choice 2
1. Evaluate the right subtree first, leaving the result in a register.
During this evaluation we shall require upto l2 registers.
2. Evaluate the left subtree. During this, we might require upto
l1 + 1 registers.
I The maximum register requirement over the whole tree is
max(l1 + 1, l2) = l2
Therefore the subtree requiring more registers should be
evaluated first.
![Page 36: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/36.jpg)
Key Idea
I Choice 2
1. Evaluate the right subtree first, leaving the result in a register.
During this evaluation we shall require upto l2 registers.
2. Evaluate the left subtree. During this, we might require upto
l1 + 1 registers.
I The maximum register requirement over the whole tree is
max(l1 + 1, l2) = l2
Therefore the subtree requiring more registers should be
evaluated first.
![Page 37: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/37.jpg)
Key Idea
I Choice 2
1. Evaluate the right subtree first, leaving the result in a register.
During this evaluation we shall require upto l2 registers.
2. Evaluate the left subtree. During this, we might require upto
l1 + 1 registers.
I The maximum register requirement over the whole tree is
max(l1 + 1, l2) = l2
Therefore the subtree requiring more registers should be
evaluated first.
![Page 38: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/38.jpg)
Key Idea
I Choice 2
1. Evaluate the right subtree first, leaving the result in a register.
During this evaluation we shall require upto l2 registers.
2. Evaluate the left subtree. During this, we might require upto
l1 + 1 registers.
I The maximum register requirement over the whole tree is
max(l1 + 1, l2) = l2
Therefore the subtree requiring more registers should be
evaluated first.
![Page 39: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/39.jpg)
Labeling the Expression Tree
I Label each node by the number of registers required to
evaluate it in a store free manner.
/ *
+ +a
b c
c
d e
2
3
2
1
1
0 1 0
11 1
_
I Left and the right leaves are labeled 1 and 0 respectively,
because the left leaf must necessarily be in a register, whereas
the right leaf can reside in memory.
![Page 40: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/40.jpg)
Labeling the Expression Tree
I Label each node by the number of registers required to
evaluate it in a store free manner.
/ *
+ +a
b c
c
d e
2
3
2
1
1
0 1 0
11 1
_
I Left and the right leaves are labeled 1 and 0 respectively,
because the left leaf must necessarily be in a register, whereas
the right leaf can reside in memory.
![Page 41: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/41.jpg)
Labeling the Expression Tree
I Label each node by the number of registers required to
evaluate it in a store free manner.
/ *
+ +a
b c
c
d e
2
3
2
1
1
0 1 0
11 1
_
I Left and the right leaves are labeled 1 and 0 respectively,
because the left leaf must necessarily be in a register, whereas
the right leaf can reside in memory.
![Page 42: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/42.jpg)
Labeling the Expression Tree
I Label each node by the number of registers required to
evaluate it in a store free manner.
/ *
+ +a
b c
c
d e
2
3
2
1
1
0 1 0
11 1
_
I Left and the right leaves are labeled 1 and 0 respectively,
because the left leaf must necessarily be in a register, whereas
the right leaf can reside in memory.
![Page 43: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/43.jpg)
Labeling the Expression Tree
I Visit the tree in post-order. For every node visited do:
1. Label each left leaf by 1 and each right leaf by 0.
2. If the labels of the children of a node n are l1 and l2
respectively, then
label(n) = max(l1, l2), if l1 6= l2
= l1 + 1, otherwise
![Page 44: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/44.jpg)
Labeling the Expression Tree
I Visit the tree in post-order. For every node visited do:
1. Label each left leaf by 1 and each right leaf by 0.
2. If the labels of the children of a node n are l1 and l2
respectively, then
label(n) = max(l1, l2), if l1 6= l2
= l1 + 1, otherwise
![Page 45: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/45.jpg)
Labeling the Expression Tree
I Visit the tree in post-order. For every node visited do:
1. Label each left leaf by 1 and each right leaf by 0.
2. If the labels of the children of a node n are l1 and l2
respectively, then
label(n) = max(l1, l2), if l1 6= l2
= l1 + 1, otherwise
![Page 46: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/46.jpg)
Assumptions and Notational Conventions
1. The code generation algorithm is represented as a function
gencode(n), which produces code to evaluate the node
labeled n.
2. Register allocation is done from a stack of register names
rstack, initially containing r0, r1, . . . , rk (with r0 on top of the
stack).
3. gencode(n) evaluates n in the register on the top of the stack.
4. Temporary allocation is done from a stack of temporary
names tstack , initially containing t0, t1, . . . , tk (with t0 on top
of the stack).
5. swap(rstack) swaps the top two registers on the stack.
![Page 47: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/47.jpg)
Assumptions and Notational Conventions
1. The code generation algorithm is represented as a function
gencode(n), which produces code to evaluate the node
labeled n.
2. Register allocation is done from a stack of register names
rstack , initially containing r0, r1, . . . , rk (with r0 on top of the
stack).
3. gencode(n) evaluates n in the register on the top of the stack.
4. Temporary allocation is done from a stack of temporary
names tstack , initially containing t0, t1, . . . , tk (with t0 on top
of the stack).
5. swap(rstack) swaps the top two registers on the stack.
![Page 48: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/48.jpg)
Assumptions and Notational Conventions
1. The code generation algorithm is represented as a function
gencode(n), which produces code to evaluate the node
labeled n.
2. Register allocation is done from a stack of register names
rstack , initially containing r0, r1, . . . , rk (with r0 on top of the
stack).
3. gencode(n) evaluates n in the register on the top of the stack.
4. Temporary allocation is done from a stack of temporary
names tstack , initially containing t0, t1, . . . , tk (with t0 on top
of the stack).
5. swap(rstack) swaps the top two registers on the stack.
![Page 49: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/49.jpg)
Assumptions and Notational Conventions
1. The code generation algorithm is represented as a function
gencode(n), which produces code to evaluate the node
labeled n.
2. Register allocation is done from a stack of register names
rstack , initially containing r0, r1, . . . , rk (with r0 on top of the
stack).
3. gencode(n) evaluates n in the register on the top of the stack.
4. Temporary allocation is done from a stack of temporary
names tstack , initially containing t0, t1, . . . , tk (with t0 on top
of the stack).
5. swap(rstack) swaps the top two registers on the stack.
![Page 50: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/50.jpg)
Assumptions and Notational Conventions
1. The code generation algorithm is represented as a function
gencode(n), which produces code to evaluate the node
labeled n.
2. Register allocation is done from a stack of register names
rstack , initially containing r0, r1, . . . , rk (with r0 on top of the
stack).
3. gencode(n) evaluates n in the register on the top of the stack.
4. Temporary allocation is done from a stack of temporary
names tstack , initially containing t0, t1, . . . , tk (with t0 on top
of the stack).
5. swap(rstack) swaps the top two registers on the stack.
![Page 51: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/51.jpg)
The Algorithm
I gencode(n) described by case analysis on the type of the node
n.
1. n is a left leaf:
n
name
gen(top(rstack)← name)
Comments: n is named by a variable say name. Code is
generated to load name into a register.
![Page 52: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/52.jpg)
The Algorithm
I gencode(n) described by case analysis on the type of the node
n.
1. n is a left leaf:
n
name
gen(top(rstack)← name)
Comments: n is named by a variable say name. Code is
generated to load name into a register.
![Page 53: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/53.jpg)
The Algorithm
I gencode(n) described by case analysis on the type of the node
n.
1. n is a left leaf:
n
name
gen(top(rstack)← name)
Comments: n is named by a variable say name. Code is
generated to load name into a register.
![Page 54: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/54.jpg)
The Algorithm
I gencode(n) described by case analysis on the type of the node
n.
1. n is a left leaf:
n
name
gen(top(rstack)← name)
Comments: n is named by a variable say name. Code is
generated to load name into a register.
![Page 55: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/55.jpg)
The Algorithm
2. n’s right child is a leaf:
name
n
nn1 2
op
gencode(n1 )
gen(top(rstack)← top(rstack) op name)
Comments: n1 is first evaluated in the register on the top of
the stack, followed by the operation op leaving the result in
the same register.
![Page 56: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/56.jpg)
The Algorithm
2. n’s right child is a leaf:
name
n
nn1 2
op
gencode(n1 )
gen(top(rstack)← top(rstack) op name)
Comments: n1 is first evaluated in the register on the top of
the stack, followed by the operation op leaving the result in
the same register.
![Page 57: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/57.jpg)
The Algorithm
2. n’s right child is a leaf:
name
n
nn1 2
op
gencode(n1 )
gen(top(rstack)← top(rstack) op name)
Comments: n1 is first evaluated in the register on the top of
the stack, followed by the operation op leaving the result in
the same register.
![Page 58: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/58.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 59: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/59.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 60: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/60.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 61: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/61.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 62: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/62.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 63: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/63.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 64: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/64.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 65: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/65.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 66: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/66.jpg)
The Algorithm
3. The left child of n requires lesser number of registers. This
requirement is strictly less than the available number of
registers
n
1n
2n
op
swap(rstack); Right child goes into next to top register
gencode(n2); Evaluate right child
R := pop(rstack);
gencode(n1); Evaluate left child
gen(top(rstack)← top(rstack) op R); Issue op
push(rstack ,R);
swap(rstack) Restore register stack
![Page 67: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/67.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 68: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/68.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 69: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/69.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 70: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/70.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 71: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/71.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 72: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/72.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 73: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/73.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 74: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/74.jpg)
The Algorithm
4. The right child of n requires lesser (or the same) number of
registers than the left child, and this requirement is strictly
less than the available number of registers
n
1n
2n
op
gencode(n1);
R := pop(rstack);
gencode(n2);
gen(R ← R op top(rstack));
push(rstack ,R)
Comments: Same as case 3, except that the left sub-tree is
evaluated first.
![Page 75: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/75.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 76: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/76.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 77: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/77.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 78: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/78.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 79: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/79.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 80: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/80.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 81: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/81.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 82: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/82.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 83: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/83.jpg)
The Algorithm
5. Both the children of n require registers greater or equal to the
available number of registers.
n
1n
2n
op
gencode(n2);
T := pop(tstack);
gen(T ← top(rstack));
gencode(n1);
push(tstack ,T );
gen(top(rstack)← top(rstack) op T );
Comments: In this case the right sub-tree is first evaluated into a
temporary. This is followed by the evaluations of the left sub-tree
and n into the register on the top of the stack.
![Page 84: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/84.jpg)
An Example
For the example:
/ *
+ +a
b c
c
d e
2
3
2
1
1
0 1 0
11 1
_
assuming two available registers r0 and r1, the calls to gencode and
the generated code are shown on the next slide.
![Page 85: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/85.jpg)
An Example
For the example:
/ *
+ +a
b c
c
d e
2
3
2
1
1
0 1 0
11 1
_
assuming two available registers r0 and r1, the calls to gencode and
the generated code are shown on the next slide.
![Page 86: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/86.jpg)
An Example
gencode(/)
SUB t1,r0
gencode(*)
MOVE r0,t1
[r0,r1]
[r0,r1]
gencode(-)[r0,r1]
gencode(+)
MUL r1,r0
gencode(+)
DIV r1,r0
[r1]
[r1]
gencode(a)
gencode(c)[r0]
[r0]
MOVE c,r0
MOVE a,r0
ADD e,r1
ADD c,r1
gencode(b)
gencode(d)[r1]
[r1]
MOVE d,r1
MOVE b,r1
![Page 87: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/87.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
I The algorithm is optimal because
1. The number of load instructions generated is optimal.
2. Each binary operation specified in the expression tree is
performed only once.
3. The number of stores is optimal.
I We shall now elaborate on each of these.
![Page 88: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/88.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
I The algorithm is optimal because
1. The number of load instructions generated is optimal.
2. Each binary operation specified in the expression tree is
performed only once.
3. The number of stores is optimal.
I We shall now elaborate on each of these.
![Page 89: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/89.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
I The algorithm is optimal because
1. The number of load instructions generated is optimal.
2. Each binary operation specified in the expression tree is
performed only once.
3. The number of stores is optimal.
I We shall now elaborate on each of these.
![Page 90: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/90.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
I The algorithm is optimal because
1. The number of load instructions generated is optimal.
2. Each binary operation specified in the expression tree is
performed only once.
3. The number of stores is optimal.
I We shall now elaborate on each of these.
![Page 91: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/91.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
I The algorithm is optimal because
1. The number of load instructions generated is optimal.
2. Each binary operation specified in the expression tree is
performed only once.
3. The number of stores is optimal.
I We shall now elaborate on each of these.
![Page 92: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/92.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
1. It is easy to verify that the number of loads required by any
program computing an expression tree is at least equal to the
number of left leaves. This algorithm generates no more loads
than this.
2. Each node of the expression tree is visited exactly once. If this
node specifies a binary operation, then the algorithm branches
into steps 2,3,4 or 5, and at each of these places code is
generated to perform this operation exactly once.
![Page 93: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/93.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
1. It is easy to verify that the number of loads required by any
program computing an expression tree is at least equal to the
number of left leaves. This algorithm generates no more loads
than this.
2. Each node of the expression tree is visited exactly once. If this
node specifies a binary operation, then the algorithm branches
into steps 2,3,4 or 5, and at each of these places code is
generated to perform this operation exactly once.
![Page 94: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/94.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
3. The number of stores is optimal: this is harder to show.
I Define a major node as a node, each of whose children has a
label at least equal to the number of available registers.
I If we can show that the number of stores required by any
program computing an expression tree is at least equal the
number of major nodes, then our algorithm produces minimal
number of stores (Why?)
![Page 95: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/95.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
3. The number of stores is optimal: this is harder to show.
I Define a major node as a node, each of whose children has a
label at least equal to the number of available registers.
I If we can show that the number of stores required by any
program computing an expression tree is at least equal the
number of major nodes, then our algorithm produces minimal
number of stores (Why?)
![Page 96: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/96.jpg)
SETHI-ULLMAN ALGORITHM: OPTIMALITY
3. The number of stores is optimal: this is harder to show.
I Define a major node as a node, each of whose children has a
label at least equal to the number of available registers.
I If we can show that the number of stores required by any
program computing an expression tree is at least equal the
number of major nodes, then our algorithm produces minimal
number of stores (Why?)
![Page 97: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/97.jpg)
SETHI-ULLMAN ALGORITHM
I To see this, consider an expression tree and the code
generated by any optimal algorithm for this tree.
I Assume that the tree has M major nodes.
I Now consider a tree formed by replacing the subtree S
evaluated by the first store, with a leaf labeled by a name l .
2
S
n
n
l1
n
I Let n be the major node in the original tree, just above S , and
n1 and n2 be its immediate descendants (n1 could be l itself).
![Page 98: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/98.jpg)
SETHI-ULLMAN ALGORITHM
I To see this, consider an expression tree and the code
generated by any optimal algorithm for this tree.
I Assume that the tree has M major nodes.
I Now consider a tree formed by replacing the subtree S
evaluated by the first store, with a leaf labeled by a name l .
2
S
n
n
l1
n
I Let n be the major node in the original tree, just above S , and
n1 and n2 be its immediate descendants (n1 could be l itself).
![Page 99: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/99.jpg)
SETHI-ULLMAN ALGORITHM
I To see this, consider an expression tree and the code
generated by any optimal algorithm for this tree.
I Assume that the tree has M major nodes.
I Now consider a tree formed by replacing the subtree S
evaluated by the first store, with a leaf labeled by a name l .
2
S
n
n
l1
n
I Let n be the major node in the original tree, just above S , and
n1 and n2 be its immediate descendants (n1 could be l itself).
![Page 100: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/100.jpg)
SETHI-ULLMAN ALGORITHM
I To see this, consider an expression tree and the code
generated by any optimal algorithm for this tree.
I Assume that the tree has M major nodes.
I Now consider a tree formed by replacing the subtree S
evaluated by the first store, with a leaf labeled by a name l .
2
S
n
n
l1
n
I Let n be the major node in the original tree, just above S , and
n1 and n2 be its immediate descendants (n1 could be l itself).
![Page 101: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/101.jpg)
SETHI-ULLMAN ALGORITHM
I To see this, consider an expression tree and the code
generated by any optimal algorithm for this tree.
I Assume that the tree has M major nodes.
I Now consider a tree formed by replacing the subtree S
evaluated by the first store, with a leaf labeled by a name l .
2
S
n
n
l1
n
I Let n be the major node in the original tree, just above S , and
n1 and n2 be its immediate descendants (n1 could be l itself).
![Page 102: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/102.jpg)
SETHI-ULLMAN ALGORITHM
1. In the modified tree, the (modified) label of n1 might have
decreased but the label of n2 remains unaffected (≥ k , the
available number of registers).
2. The label of n is ≥ k .
3. The node n may no longer be a major node but all other
major nodes in the original tree continue to be major nodes in
the modified tree.
4. Therefore the number of major nodes in the modified tree is
M − 1.
5. If we assume as induction hypothesis that the number of
stores for the modified tree is at least M − 1, then the number
of stores for the original tree is at least M.
![Page 103: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/103.jpg)
SETHI-ULLMAN ALGORITHM
1. In the modified tree, the (modified) label of n1 might have
decreased but the label of n2 remains unaffected (≥ k , the
available number of registers).
2. The label of n is ≥ k .
3. The node n may no longer be a major node but all other
major nodes in the original tree continue to be major nodes in
the modified tree.
4. Therefore the number of major nodes in the modified tree is
M − 1.
5. If we assume as induction hypothesis that the number of
stores for the modified tree is at least M − 1, then the number
of stores for the original tree is at least M.
![Page 104: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/104.jpg)
SETHI-ULLMAN ALGORITHM
1. In the modified tree, the (modified) label of n1 might have
decreased but the label of n2 remains unaffected (≥ k , the
available number of registers).
2. The label of n is ≥ k .
3. The node n may no longer be a major node but all other
major nodes in the original tree continue to be major nodes in
the modified tree.
4. Therefore the number of major nodes in the modified tree is
M − 1.
5. If we assume as induction hypothesis that the number of
stores for the modified tree is at least M − 1, then the number
of stores for the original tree is at least M.
![Page 105: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/105.jpg)
SETHI-ULLMAN ALGORITHM
1. In the modified tree, the (modified) label of n1 might have
decreased but the label of n2 remains unaffected (≥ k , the
available number of registers).
2. The label of n is ≥ k .
3. The node n may no longer be a major node but all other
major nodes in the original tree continue to be major nodes in
the modified tree.
4. Therefore the number of major nodes in the modified tree is
M − 1.
5. If we assume as induction hypothesis that the number of
stores for the modified tree is at least M − 1, then the number
of stores for the original tree is at least M.
![Page 106: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/106.jpg)
SETHI-ULLMAN ALGORITHM
1. In the modified tree, the (modified) label of n1 might have
decreased but the label of n2 remains unaffected (≥ k , the
available number of registers).
2. The label of n is ≥ k .
3. The node n may no longer be a major node but all other
major nodes in the original tree continue to be major nodes in
the modified tree.
4. Therefore the number of major nodes in the modified tree is
M − 1.
5. If we assume as induction hypothesis that the number of
stores for the modified tree is at least M − 1, then the number
of stores for the original tree is at least M.
![Page 107: Code Generation: Sethi Ullman Algorithm - GitHub PagesSethi-Ullman Algorithm { Introduction I Generates code for expression trees (not dags). I Target machine model is simple. Has](https://reader034.fdocuments.net/reader034/viewer/2022052521/60b30f3de40b5a04f70a9ff4/html5/thumbnails/107.jpg)
SETHI-ULLMAN ALGORITHM: COMPLEXITY
Since the algorithm visits every node of the expression tree twice –
once during labeling, and once during code generation, the
complexity of the algorithm is O(n).