An expression that cannot be evaluated using just 2 registers, but can be evaluated using 3 registers:
expression | AST | pseudo code |
---|---|---|
(a - b) - (c - d) |
- / \ - - / \ / \ a b c d |
load a into R0 load b into R1 R0 = R0 - R1 load c into R1 load b into R2 R1 = R1 - R2 R0 = R0 - R1 |
An expression that cannot be evaluated using just 3 registers,but can be evaluated using 4 registers:
expression | AST | pseudo code |
---|---|---|
((a - b) - (c - d)) - ((e - f) - (g - h)) |
- / \ / \ / \ - - / \ / \ - - - - / \ / \ / \ / \ a b c d e f g h |
load a into R0 load b into R1 R0 = R0 - R1 load c into R1 load d into R2 R1 = R1 - R2 R0 = R0 - R1 load e into R1 load f into R2 R1 = R1 - R2 load g into R2 load h into R3 R2 = R2 - R3 R1 = R1 - R2 R0 = R0 - R1 |
int numRegisters(ASTNode node) { if isLeaf(node) return 1 else N1 = numRegisters(node.leftChild) N2 = numRegisters(node.rightChild) if (N1 == N2) return N1 + 1 else return max(N1, N2) }
Last Updated: 4/11/2024 © 2024 CS 536