code generation by tree walking - github pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

67
Code Generation by Tree Walking Amey Karkare [email protected] April 10, 2019

Upload: others

Post on 27-Mar-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

Amey Karkare

[email protected]

April 10, 2019

Page 2: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I Pushes dynamic programming to a pre-processing stage prior

to code-generation time.

I Simplifies dynamic programming effort by assuming

unbounded number of registers.

I Only cases taken into account are different patterns matching

a node.

I Normalization of costs

Page 3: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I Pushes dynamic programming to a pre-processing stage prior

to code-generation time.

I Simplifies dynamic programming effort by assuming

unbounded number of registers.

I Only cases taken into account are different patterns matching

a node.

I Normalization of costs

Page 4: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I Pushes dynamic programming to a pre-processing stage prior

to code-generation time.

I Simplifies dynamic programming effort by assuming

unbounded number of registers.

I Only cases taken into account are different patterns matching

a node.

I Normalization of costs

Page 5: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I Pushes dynamic programming to a pre-processing stage prior

to code-generation time.

I Simplifies dynamic programming effort by assuming

unbounded number of registers.

I Only cases taken into account are different patterns matching

a node.

I Normalization of costs

Page 6: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking – Example

I An example expression tree and an example machine:

Reg

+

Fetch

Fetch

Int

1 goal <− reg

reg <− Reg

addr <− reg

8

7

6

5

4

3

2 0

0

1

2

0

0

addr <− Plus

reg

reg <− Plus

regreg

reg <− Fetch

addr

9 int <− Int

int

reg <− int

addr <− int

1

0

3

rule no

rule

terminal

non−terminalpattern

rule cost

Page 7: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking – Example

I The tree can be covered in more than one ways

rule 4 (reg)rule 1 (goal)

rule 4 (reg)rule 6 (addr)

rule 5 (reg)rule 6 (addr)

rule 2 (reg) Reg

Fetch

Fetch

Intrule 2 (reg)

rule 4 (reg)rule 1 (goal)

rule 8 (addr)

rule 4 (reg)rule 6 (addr)

Reg

Fetch

Fetch

Int

Plus Plus

rule 3 (reg)rule 9 (int)

rule 9 (int)

Cost = 7Cost = 6

I We are finally interested in the least cost tree.

I We also want to do some pre-processing before we get any

tree,

Page 8: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking – Example

I The tree can be covered in more than one ways

rule 4 (reg)rule 1 (goal)

rule 4 (reg)rule 6 (addr)

rule 5 (reg)rule 6 (addr)

rule 2 (reg) Reg

Fetch

Fetch

Intrule 2 (reg)

rule 4 (reg)rule 1 (goal)

rule 8 (addr)

rule 4 (reg)rule 6 (addr)

Reg

Fetch

Fetch

Int

Plus Plus

rule 3 (reg)rule 9 (int)

rule 9 (int)

Cost = 7Cost = 6

I We are finally interested in the least cost tree.

I We also want to do some pre-processing before we get any

tree,

Page 9: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking – Example

I The tree can be covered in more than one ways

rule 4 (reg)rule 1 (goal)

rule 4 (reg)rule 6 (addr)

rule 5 (reg)rule 6 (addr)

rule 2 (reg) Reg

Fetch

Fetch

Intrule 2 (reg)

rule 4 (reg)rule 1 (goal)

rule 8 (addr)

rule 4 (reg)rule 6 (addr)

Reg

Fetch

Fetch

Int

Plus Plus

rule 3 (reg)rule 9 (int)

rule 9 (int)

Cost = 7Cost = 6

I We are finally interested in the least cost tree.

I We also want to do some pre-processing before we get any

tree,

Page 10: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking – Example

I How is this done? Given a tree,

I traverse the tree bottom up. With the help

of a transition table, annotate each node of the tree with a state.

reg <− int, 1goal <− reg, 1addr <− reg, 1int <− Int, 0

reg <− Reg, 0goal <− reg, 0addr <− reg, 0

reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Fetch(addr), 4goal <− reg, 4addr <− reg, 4

reg <− Fetch(addr), 6goal <− reg, 6addr <− reg, 6

Reg

Fetch

Fetch

Int

Plus

state 2:state 1:

state 5:

state 4:

state 3:

Page 11: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking – Example

I How is this done? Given a tree,

I traverse the tree bottom up. With the help

of a transition table, annotate each node of the tree with a state.

reg <− int, 1goal <− reg, 1addr <− reg, 1int <− Int, 0

reg <− Reg, 0goal <− reg, 0addr <− reg, 0

reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Fetch(addr), 4goal <− reg, 4addr <− reg, 4

reg <− Fetch(addr), 6goal <− reg, 6addr <− reg, 6

Reg

Fetch

Fetch

Int

Plus

state 2:state 1:

state 5:

state 4:

state 3:

Page 12: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I State: Gives the minimum cost of evaluating a node in the

expression tree to different non-terminals.

I Transition table: Gives

I state corresponding to leaf nodes (0-ary terminals).

I given the states of children, gives state of interior nodes (n-ary

terminals).

Page 13: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I State: Gives the minimum cost of evaluating a node in the

expression tree to different non-terminals.

I Transition table: Gives

I state corresponding to leaf nodes (0-ary terminals).

I given the states of children, gives state of interior nodes (n-ary

terminals).

Page 14: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I State: Gives the minimum cost of evaluating a node in the

expression tree to different non-terminals.

I Transition table: Gives

I state corresponding to leaf nodes (0-ary terminals).

I given the states of children, gives state of interior nodes (n-ary

terminals).

Page 15: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking

I State: Gives the minimum cost of evaluating a node in the

expression tree to different non-terminals.

I Transition table: Gives

I state corresponding to leaf nodes (0-ary terminals).

I given the states of children, gives state of interior nodes (n-ary

terminals).

Page 16: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Code Generation by Tree Walking – Example

I A second top-down pass determines the instructions to be

used at each node assuming that the root is to be evaluated

in goal.

Reg

Fetch

Fetch

Int

Plus

state 2:state 1:

state 5:

state 4:

state 3:

reg <− Fetch(addr), 6goal <− reg, 6addr <− reg, 6

reg <− Fetch(addr), 4goal <− reg, 4addr <− reg, 4

reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− int, 1goal <− reg, 1addr <− reg, 1int <− Int, 0

reg <− Reg, 0goal <− reg, 0addr <− reg, 0

Page 17: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I For 0-ary terminals

I Find least cost covering rules. A covering rule can cover the

terminal with its pattern.

I Find least cost chain rules. A chain rule is of the form

nonterminal ← nonterminal .Int goal <− reg, 1

reg <− int, 1int <− Int, 0addr <− reg, 1

−− chain rule

−− covering rule−− chain rule

−− chain rule

I Cost of reducing Int to goal is

cost of reducing Int to int (0) +

cost of reducing int to reg (1) +

cost of reducing reg to goal (0) +

Page 18: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I For 0-ary terminals

I Find least cost covering rules. A covering rule can cover the

terminal with its pattern.

I Find least cost chain rules. A chain rule is of the form

nonterminal ← nonterminal .Int goal <− reg, 1

reg <− int, 1int <− Int, 0addr <− reg, 1

−− chain rule

−− covering rule−− chain rule

−− chain rule

I Cost of reducing Int to goal is

cost of reducing Int to int (0) +

cost of reducing int to reg (1) +

cost of reducing reg to goal (0) +

Page 19: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I For 0-ary terminals

I Find least cost covering rules. A covering rule can cover the

terminal with its pattern.

I Find least cost chain rules. A chain rule is of the form

nonterminal ← nonterminal .Int goal <− reg, 1

reg <− int, 1int <− Int, 0addr <− reg, 1

−− chain rule

−− covering rule−− chain rule

−− chain rule

I Cost of reducing Int to goal is

cost of reducing Int to int (0) +

cost of reducing int to reg (1) +

cost of reducing reg to goal (0) +

Page 20: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I For 0-ary terminals

I Find least cost covering rules. A covering rule can cover the

terminal with its pattern.

I Find least cost chain rules. A chain rule is of the form

nonterminal ← nonterminal .Int goal <− reg, 1

reg <− int, 1int <− Int, 0addr <− reg, 1

−− chain rule

−− covering rule−− chain rule

−− chain rule

I Cost of reducing Int to goal is

cost of reducing Int to int (0) +

cost of reducing int to reg (1) +

cost of reducing reg to goal (0) +

Page 21: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I n-ary terminals

I If both children of Plus are in state 2, in which state would

Plus be?

I The rule reg ← Plus(reg, reg) gives

reg <− Plus(reg, reg), 2goal <− reg, 2

addr <− reg, 2Plus

reg regstate 2:

int <− Int, 0 addr <− reg, 1goal <− reg, 1reg <− int, 1 state 2:

int <− Int, 0

goal <− reg, 1addr <− reg, 1

reg <− int, 1

Page 22: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I n-ary terminals

I If both children of Plus are in state 2, in which state would

Plus be?

I The rule reg ← Plus(reg, reg) gives

reg <− Plus(reg, reg), 2goal <− reg, 2

addr <− reg, 2Plus

reg regstate 2:

int <− Int, 0 addr <− reg, 1goal <− reg, 1reg <− int, 1 state 2:

int <− Int, 0

goal <− reg, 1addr <− reg, 1

reg <− int, 1

Page 23: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I n-ary terminals

I If both children of Plus are in state 2, in which state would

Plus be?

I The rule reg ← Plus(reg, reg) gives

reg <− Plus(reg, reg), 2goal <− reg, 2

addr <− reg, 2Plus

reg regstate 2:

int <− Int, 0 addr <− reg, 1goal <− reg, 1reg <− int, 1 state 2:

int <− Int, 0

goal <− reg, 1addr <− reg, 1

reg <− int, 1

Page 24: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I The rule reg ← Plus(reg, int) gives

Plus

reg state 2:

int <− Int, 0 addr <− reg, 1goal <− reg, 1reg <− int, 1 state 2:

int <− Int, 0

goal <− reg, 1addr <− reg, 1

reg <− int, 1int

reg <− Plus(reg, int), 3goal <− reg, 3

addr <− reg, 3

I Conclusion: If the leaves of Plus are both in state 2, then Plus

will be in

reg <− Plus(reg, reg), 2goal <− reg, 2

addr <− reg, 2

state 6:

Page 25: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I The rule reg ← Plus(reg, int) gives

Plus

reg state 2:

int <− Int, 0 addr <− reg, 1goal <− reg, 1reg <− int, 1 state 2:

int <− Int, 0

goal <− reg, 1addr <− reg, 1

reg <− int, 1int

reg <− Plus(reg, int), 3goal <− reg, 3

addr <− reg, 3

I Conclusion: If the leaves of Plus are both in state 2, then Plus

will be in

reg <− Plus(reg, reg), 2goal <− reg, 2

addr <− reg, 2

state 6:

Page 26: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I Similarly, we should also find the transitions for Plus on pairs

(state1, state1), (state1, state2), (state2, state6)

I . . .

I Will this process always terminate?

Page 27: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I Similarly, we should also find the transitions for Plus on pairs

(state1, state1), (state1, state2), (state2, state6)

I . . .

I Will this process always terminate?

Page 28: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I Similarly, we should also find the transitions for Plus on pairs

(state1, state1), (state1, state2), (state2, state6)

I . . .

I Will this process always terminate?

Page 29: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I Consider computation of the state at Fetch, with reg in the

state shown.Fetch

reg reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

I Successive computation of the states for Fetch yield:

Fetch

reg

reg <− Fetch( addr), 2goal <− reg, 2addr <− reg, 2

reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

Fetch

reg reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Fetch( addr), 4goal <− reg, 4addr <− reg, 4

and so on . . .

Page 30: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Precomputing the Transition Table

I Consider computation of the state at Fetch, with reg in the

state shown.Fetch

reg reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

I Successive computation of the states for Fetch yield:

Fetch

reg

reg <− Fetch( addr), 2goal <− reg, 2addr <− reg, 2

reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

Fetch

reg reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Fetch( addr), 4goal <− reg, 4addr <− reg, 4

and so on . . .

Page 31: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I The solution is to relativize the costs in a state with respect

to the item with the cheapest cost

I After relativization, the state on the left changes to the state

on the right:reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

I Does this make the resulting transition table different?

Obviously not.

I Does this necessarily lead to a finite number of states?

Page 32: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I The solution is to relativize the costs in a state with respect

to the item with the cheapest cost

I After relativization, the state on the left changes to the state

on the right:reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

I Does this make the resulting transition table different?

Obviously not.

I Does this necessarily lead to a finite number of states?

Page 33: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I The solution is to relativize the costs in a state with respect

to the item with the cheapest cost

I After relativization, the state on the left changes to the state

on the right:reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

I Does this make the resulting transition table different?

Obviously not.

I Does this necessarily lead to a finite number of states?

Page 34: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I The solution is to relativize the costs in a state with respect

to the item with the cheapest cost

I After relativization, the state on the left changes to the state

on the right:reg <− Plus(reg, reg), 2goal <− reg, 2addr <− reg, 2

reg <− Plus(reg, reg), 0goal <− reg, 0addr <− reg, 0

I Does this make the resulting transition table different?

Obviously not.

I Does this necessarily lead to a finite number of states?

Page 35: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Consider a machine with only these two instructions involving

Fetch. Fetch

reg

reg

cost = 1

Fetch

int

int

cost = 3

I Consider a state in which the reg ← . . . item is 2 cheaper than

the int ← . . . item.

I Transits to a state in which the reg ← . . . item is 4 cheaper

than the int ← . . . item.

I . . .

I Practical solution: If cost difference between any pair of

terminals is greater than a threshold, instruction set is

rejected.

I Typical instruction sets do not lead to divergence.

Page 36: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Consider a machine with only these two instructions involving

Fetch. Fetch

reg

reg

cost = 1

Fetch

int

int

cost = 3

I Consider a state in which the reg ← . . . item is 2 cheaper than

the int ← . . . item.

I Transits to a state in which the reg ← . . . item is 4 cheaper

than the int ← . . . item.

I . . .

I Practical solution: If cost difference between any pair of

terminals is greater than a threshold, instruction set is

rejected.

I Typical instruction sets do not lead to divergence.

Page 37: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Consider a machine with only these two instructions involving

Fetch. Fetch

reg

reg

cost = 1

Fetch

int

int

cost = 3

I Consider a state in which the reg ← . . . item is 2 cheaper than

the int ← . . . item.

I Transits to a state in which the reg ← . . . item is 4 cheaper

than the int ← . . . item.

I . . .

I Practical solution: If cost difference between any pair of

terminals is greater than a threshold, instruction set is

rejected.

I Typical instruction sets do not lead to divergence.

Page 38: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Consider a machine with only these two instructions involving

Fetch. Fetch

reg

reg

cost = 1

Fetch

int

int

cost = 3

I Consider a state in which the reg ← . . . item is 2 cheaper than

the int ← . . . item.

I Transits to a state in which the reg ← . . . item is 4 cheaper

than the int ← . . . item.

I . . .

I Practical solution: If cost difference between any pair of

terminals is greater than a threshold, instruction set is

rejected.

I Typical instruction sets do not lead to divergence.

Page 39: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Consider a machine with only these two instructions involving

Fetch. Fetch

reg

reg

cost = 1

Fetch

int

int

cost = 3

I Consider a state in which the reg ← . . . item is 2 cheaper than

the int ← . . . item.

I Transits to a state in which the reg ← . . . item is 4 cheaper

than the int ← . . . item.

I . . .

I Practical solution: If cost difference between any pair of

terminals is greater than a threshold, instruction set is

rejected.

I Typical instruction sets do not lead to divergence.

Page 40: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Consider a machine with only these two instructions involving

Fetch. Fetch

reg

reg

cost = 1

Fetch

int

int

cost = 3

I Consider a state in which the reg ← . . . item is 2 cheaper than

the int ← . . . item.

I Transits to a state in which the reg ← . . . item is 4 cheaper

than the int ← . . . item.

I . . .

I Practical solution: If cost difference between any pair of

terminals is greater than a threshold, instruction set is

rejected.

I Typical instruction sets do not lead to divergence.

Page 41: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Naively generated transition tables are very large.

I Typical CISC machine (1995 vintage) will generate 1000

states.

I Two states can be merged if the difference is not important in

all possible situtaions

I Two major optimizations

I State reduction by projecting out irrelevant items

I State reduction by triangle trimming.

Page 42: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Naively generated transition tables are very large.

I Typical CISC machine (1995 vintage) will generate 1000

states.

I Two states can be merged if the difference is not important in

all possible situtaions

I Two major optimizations

I State reduction by projecting out irrelevant items

I State reduction by triangle trimming.

Page 43: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Naively generated transition tables are very large.

I Typical CISC machine (1995 vintage) will generate 1000

states.

I Two states can be merged if the difference is not important in

all possible situtaions

I Two major optimizations

I State reduction by projecting out irrelevant items

I State reduction by triangle trimming.

Page 44: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Naively generated transition tables are very large.

I Typical CISC machine (1995 vintage) will generate 1000

states.

I Two states can be merged if the difference is not important in

all possible situtaions

I Two major optimizations

I State reduction by projecting out irrelevant items

I State reduction by triangle trimming.

Page 45: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Naively generated transition tables are very large.

I Typical CISC machine (1995 vintage) will generate 1000

states.

I Two states can be merged if the difference is not important in

all possible situtaions

I Two major optimizations

I State reduction by projecting out irrelevant items

I State reduction by triangle trimming.

Page 46: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

Relativization of states

I Naively generated transition tables are very large.

I Typical CISC machine (1995 vintage) will generate 1000

states.

I Two states can be merged if the difference is not important in

all possible situtaions

I Two major optimizations

I State reduction by projecting out irrelevant items

I State reduction by triangle trimming.

Page 47: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Projecting out Irrelevant Items

I Consider a machine in which the only instructions involving

Plus are:Plus

reg reg

Plus

reg int

I Also assume that there are two states:goal <− reg, 0reg <− Reg, 0addr <− reg, 0

state 1: state 2:goal <− reg, 1reg <− int, 1addr <− reg, 1int <− Int, 0

Page 48: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Projecting out Irrelevant Items

I Consider a machine in which the only instructions involving

Plus are:Plus

reg reg

Plus

reg int

I Also assume that there are two states:goal <− reg, 0reg <− Reg, 0addr <− reg, 0

state 1: state 2:goal <− reg, 1reg <− int, 1addr <− reg, 1int <− Int, 0

Page 49: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Projecting out Irrelevant Items

I The normal transition table for Plus:first argument

second argument

state 1 state 2Plus

state 1

state 2

Page 50: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Projecting out Irrelevant Items

I Since the first argument of Plus is a reg, we can project the

int ← . . . item out of both the states. The resulting transition

table for Plus is:

first argument

second argument

state 1 state 2Plus

state 1

state 2

state 1

Page 51: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Triangle Trimming

I Assume that a state s has two items nt ← . . . and nt’ ← . . . .

Under what conditions can we say that nt ← . . . is subsumed

by nt’ ← . . . and thus can be removed from s.

I Assume that the state has been used in the context of the

operator op at the argument position shown op

nt1 nt2 nt3

nti

*nt (state s)

Page 52: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Triangle Trimming

I Assume that a state s has two items nt ← . . . and nt’ ← . . . .

Under what conditions can we say that nt ← . . . is subsumed

by nt’ ← . . . and thus can be removed from s.

I Assume that the state has been used in the context of the

operator op at the argument position shown op

nt1 nt2 nt3

nti

*nt (state s)

Page 53: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Triangle Trimming

I The general situation under which nt ← . . . is subsumed by

nt’ ← . . . is: op

nt1 nt2 nt3*

nt

nt2’ nt3’

* * *nt’

nt1’

op

nti

ntj

**(s)

I The cost of the rule nti ← . . . and the black chain reductions

should be less than the rule ntj ← . . . and the red chain

reductions.

I Further this should be true in all contexts in which s can be

used.

Page 54: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Triangle Trimming

I The general situation under which nt ← . . . is subsumed by

nt’ ← . . . is: op

nt1 nt2 nt3*

nt

nt2’ nt3’

* * *nt’

nt1’

op

nti

ntj

**(s)

I The cost of the rule nti ← . . . and the black chain reductions

should be less than the rule ntj ← . . . and the red chain

reductions.

I Further this should be true in all contexts in which s can be

used.

Page 55: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

State Reduction by Triangle Trimming

I The general situation under which nt ← . . . is subsumed by

nt’ ← . . . is: op

nt1 nt2 nt3*

nt

nt2’ nt3’

* * *nt’

nt1’

op

nti

ntj

**(s)

I The cost of the rule nti ← . . . and the black chain reductions

should be less than the rule ntj ← . . . and the red chain

reductions.

I Further this should be true in all contexts in which s can be

used.

Page 56: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Bottom Up Rewriting based code Generator

I Sample BURG input.

traverse treemacros to

%{ #define NODEPTR_TYPE treepointer #define OP_LABEL(p) ((p)−>op) #define LEFT_CHILD(p) ((p) −> left) #define RIGHT_CHILD(p) ((p) −> right) #define STATE_LABEL(p) ((p) −> state_label)%} %start goal

%term Mul=5 Plus=6 %term Assign=1 Constant=2 Fetch=3 Four=4

%% con: Constant = 1 (0); con: Four = 2 (0); addr: con = 3 (0); addr: Plus(con, reg) = 4 (0); addr: Plus(con,Mul(Four,reg)) = 5 (0); reg: Fetch(addr) = 6 (1); reg: Assign(addr,reg) = 7 (1); goal: reg = 8 (0);

BURG’s name for node type user’s name for node type

cost rule number

terminals

rule

non−terminals

pattern

Page 57: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Bottom Up Rewriting based code Generator

I Sample BURG input.

traverse treemacros to

%{ #define NODEPTR_TYPE treepointer #define OP_LABEL(p) ((p)−>op) #define LEFT_CHILD(p) ((p) −> left) #define RIGHT_CHILD(p) ((p) −> right) #define STATE_LABEL(p) ((p) −> state_label)%} %start goal

%term Mul=5 Plus=6 %term Assign=1 Constant=2 Fetch=3 Four=4

%% con: Constant = 1 (0); con: Four = 2 (0); addr: con = 3 (0); addr: Plus(con, reg) = 4 (0); addr: Plus(con,Mul(Four,reg)) = 5 (0); reg: Fetch(addr) = 6 (1); reg: Assign(addr,reg) = 7 (1); goal: reg = 8 (0);

BURG’s name for node type user’s name for node type

cost rule number

terminals

rule

non−terminals

pattern

Page 58: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.

I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 59: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 60: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 61: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 62: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 63: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 64: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 65: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 66: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Two traversals over the subject tree.I Labeling traversal.

I Done entirely by generated function

burg label(NODEPTR TYPE p).

I Labels the subject tree with states (represented by integers).

I Rule selection traversal.

I Done by a wrapper function

reduce(NODEPTR TYPE p, int goalInt)

written by user around BURG generated functions.

I Starts with the root of the subject tree and the non-terminal

goal.

I At each node selects a rule for evaluating the node.

I Passes control back to user function with an integer

identifying the rule. Actions corresponding to the rule to be

managed by the user.

Page 67: Code Generation by Tree Walking - GitHub Pages · 2019. 4. 21. · 4 3 2 0 0 1 2 0 0 addr

BURG – A Code Generation Tool

I Here is an outline of a code-generator produced

with the help of BURG. Constructs in red are BURG generated.

burg_kids(p, ruleno, kids); for (i = 0; nts[i]; i++) reduce(kids[i], nts[i]);}

parse(NODEPTR_TYPE p) {

reduce(p, 1) /* and reduce it, goal = 1*/}

reduce(NODEPTR_TYPE p, int goalint) {

NODEPTR_TYPE kids[10]; int i;

burg_label(p) /* label the tree */

short *nts = burg_nts[ruleno] ;

/* ... do something with this node... */

/* process the children of this node */

int ruleno = burg_rule(STATE_LABEL(p), goalint);