Download presentation
Presentation is loading. Please wait.
1
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree
2
Abstract Syntax Tree The expression can be represented as the following tree: + b – a d c f e * + +
3
Evaluation Order Many evaluation orders are possible Typical evaluation order is depth-first from left-to-right
4
Code from Depth First L/R Traversal load a r1 Possible instructions are: sub b r1 load c r2load mem reg add d r2store mem reg load e r3OP mem reg mul f r3OP reg 1 reg 2 add r2 r3 add r1 r3
5
Tree Labeling Algorithm Start with leaf nodes Label left leaf node 1 (it will be loaded into a register) Label right leaf node 0 (it will be accessed from memory in an OP instruction) Label interior nodes (assumed to be binary operators) as follows: –If both operands of interior node have the same number n, then label the interior node n+1 –If operands of interior node have different numbers m and n where m n, label interior node max (m,n) Continue labeling until all nodes including root node are labeled
6
Labeled Tree + (2) b (0) – (1) a (1) d (0) c (1) f (0) e (1) * (1) + (1) + (2)
7
Code Generation Algorithm Start at root node Traverse the tree from node to node as follows –If labels of the operands are m and n and m n, take the path max (m,n) first –If the labels of the operand are m and n and m = n, take the leftmost path first
8
Node Visit Order (from 1 to 5) + (2) 5 b (0) – (1) 4 a (1) d (0) c (1) f (0) e (1) * (1) 2 + (1) 1 + (2) 3
9
Optimized Register Utilization load c r1 add d r1 load e r2 mul f r2 add r1 r2 load a r1 sub b r1 add r1 r2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.