<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
Sethi	B-Algorithm
–	I-Algorithm
Ullman	I-Algorithm
algorithm	I-Algorithm
is	O
an	O
algorithm	O
named	O
after	O
Ravi	O
Sethi	O
and	O
Jeffrey	O
D	O
.	O
Ullman	O
,	O
its	O
inventors	O
,	O
for	O
translating	O
abstract	B-Data_Structure
syntax	I-Data_Structure
trees	I-Data_Structure
into	O
machine	B-Language
code	I-Language
that	O
uses	O
as	O
few	O
registers	B-General_Concept
as	O
possible	O
.	O
</s>
<s>
When	O
generating	B-Application
code	I-Application
for	O
arithmetic	O
expressions	O
,	O
the	O
compiler	B-Language
has	O
to	O
decide	O
which	O
is	O
the	O
best	O
way	O
to	O
translate	O
the	O
expression	O
in	O
terms	O
of	O
number	O
of	O
instructions	O
used	O
as	O
well	O
as	O
number	O
of	O
registers	B-General_Concept
needed	O
to	O
evaluate	O
a	O
certain	O
subtree	O
.	O
</s>
<s>
Especially	O
in	O
the	O
case	O
that	O
free	O
registers	B-General_Concept
are	O
scarce	O
,	O
the	O
order	O
of	O
evaluation	O
can	O
be	O
important	O
to	O
the	O
length	O
of	O
the	O
generated	O
code	O
,	O
because	O
different	O
orderings	O
may	O
lead	O
to	O
larger	O
or	O
smaller	O
numbers	O
of	O
intermediate	O
values	O
being	O
spilled	O
to	O
memory	O
and	O
then	O
restored	O
.	O
</s>
<s>
The	O
Sethi	B-Algorithm
–	I-Algorithm
Ullman	I-Algorithm
algorithm	I-Algorithm
(	O
also	O
known	O
as	O
Sethi	O
–	O
Ullman	O
numbering	O
)	O
produces	O
code	O
which	O
needs	O
the	O
fewest	O
instructions	O
possible	O
as	O
well	O
as	O
the	O
fewest	O
storage	O
references	O
(	O
under	O
the	O
assumption	O
that	O
at	O
the	O
most	O
commutativity	O
and	O
associativity	O
apply	O
to	O
the	O
operators	O
used	O
,	O
but	O
distributive	O
laws	O
i.e.	O
</s>
<s>
The	O
simple	O
Sethi	B-Algorithm
–	I-Algorithm
Ullman	I-Algorithm
algorithm	I-Algorithm
works	O
as	O
follows	O
(	O
for	O
a	O
load/store	B-Architecture
architecture	I-Architecture
)	O
:	O
</s>
<s>
For	O
every	O
non-leaf	O
node	O
n	O
,	O
assign	O
the	O
number	O
of	O
registers	B-General_Concept
needed	O
to	O
evaluate	O
the	O
respective	O
subtrees	O
of	O
n	O
.	O
If	O
the	O
number	O
of	O
registers	B-General_Concept
needed	O
in	O
the	O
left	O
subtree	O
(	O
l	O
)	O
are	O
not	O
equal	O
to	O
the	O
number	O
of	O
registers	B-General_Concept
needed	O
in	O
the	O
right	O
subtree	O
(	O
r	O
)	O
,	O
the	O
number	O
of	O
registers	B-General_Concept
needed	O
for	O
the	O
current	O
node	O
n	O
is	O
max(l,r )	O
.	O
</s>
<s>
If	O
l	O
==	O
r	O
,	O
then	O
the	O
number	O
of	O
registers	B-General_Concept
needed	O
for	O
the	O
current	O
node	O
is	O
r+1	O
.	O
</s>
<s>
If	O
the	O
number	O
of	O
registers	B-General_Concept
needed	O
to	O
compute	O
the	O
left	O
subtree	O
of	O
node	O
n	O
is	O
bigger	O
than	O
the	O
number	O
of	O
registers	B-General_Concept
for	O
the	O
right	O
subtree	O
,	O
then	O
the	O
left	O
subtree	O
is	O
evaluated	O
first	O
(	O
since	O
it	O
may	O
be	O
possible	O
that	O
the	O
one	O
more	O
register	O
needed	O
by	O
the	O
right	O
subtree	O
to	O
save	O
the	O
result	O
makes	O
the	O
left	O
subtree	O
spill	O
)	O
.	O
</s>
<s>
If	O
the	O
right	O
subtree	O
needs	O
more	O
registers	B-General_Concept
than	O
the	O
left	O
subtree	O
,	O
the	O
right	O
subtree	O
is	O
evaluated	O
first	O
accordingly	O
.	O
</s>
<s>
If	O
both	O
subtrees	O
need	O
an	O
equal	O
number	O
of	O
registers	B-General_Concept
,	O
then	O
the	O
order	O
of	O
evaluation	O
is	O
irrelevant	O
.	O
</s>
<s>
For	O
an	O
arithmetic	O
expression	O
,	O
the	O
abstract	B-Data_Structure
syntax	I-Data_Structure
tree	I-Data_Structure
looks	O
like	O
this	O
:	O
</s>
<s>
Now	O
we	O
start	O
traversing	O
the	O
tree	O
(	O
in	O
preorder	O
for	O
now	O
)	O
,	O
assigning	O
the	O
number	O
of	O
registers	B-General_Concept
needed	O
to	O
evaluate	O
each	O
subtree	O
(	O
note	O
that	O
the	O
last	O
summand	O
in	O
the	O
expression	O
is	O
a	O
constant	O
)	O
:	O
</s>
<s>
From	O
this	O
tree	O
it	O
can	O
be	O
seen	O
that	O
we	O
need	O
2	O
registers	B-General_Concept
to	O
compute	O
the	O
left	O
subtree	O
of	O
the	O
'	O
*	O
 '	O
,	O
but	O
only	O
1	O
register	O
to	O
compute	O
the	O
right	O
subtree	O
.	O
</s>
<s>
Nodes	O
'	O
c	O
 '	O
and	O
'	O
g	O
 '	O
do	O
not	O
need	O
registers	B-General_Concept
for	O
the	O
following	O
reasons	O
:	O
If	O
T	O
is	O
a	O
tree	O
leaf	O
,	O
then	O
the	O
number	O
of	O
registers	B-General_Concept
to	O
evaluate	O
T	O
is	O
either	O
1	O
or	O
0	O
depending	O
whether	O
T	O
is	O
a	O
left	O
or	O
a	O
right	O
subtree	O
(	O
since	O
an	O
operation	O
such	O
as	O
add	O
R1	O
,	O
A	O
can	O
handle	O
the	O
right	O
component	O
A	O
directly	O
without	O
storing	O
it	O
into	O
a	O
register	O
)	O
.	O
</s>
<s>
Therefore	O
we	O
shall	O
start	O
to	O
emit	O
code	O
for	O
the	O
left	O
subtree	O
first	O
,	O
because	O
we	O
might	O
run	O
into	O
the	O
situation	O
that	O
we	O
only	O
have	O
2	O
registers	B-General_Concept
left	O
to	O
compute	O
the	O
whole	O
expression	O
.	O
</s>
<s>
If	O
we	O
now	O
computed	O
the	O
right	O
subtree	O
first	O
(	O
which	O
needs	O
only	O
1	O
register	O
)	O
,	O
we	O
would	O
then	O
need	O
a	O
register	O
to	O
hold	O
the	O
result	O
of	O
the	O
right	O
subtree	O
while	O
computing	O
the	O
left	O
subtree	O
(	O
which	O
would	O
still	O
need	O
2	O
registers	B-General_Concept
)	O
,	O
therefore	O
needing	O
3	O
registers	B-General_Concept
concurrently	O
.	O
</s>
<s>
Computing	O
the	O
left	O
subtree	O
first	O
needs	O
2	O
registers	B-General_Concept
,	O
but	O
the	O
result	O
can	O
be	O
stored	O
in	O
1	O
,	O
and	O
since	O
the	O
right	O
subtree	O
needs	O
only	O
1	O
register	O
to	O
compute	O
,	O
the	O
evaluation	O
of	O
the	O
expression	O
can	O
do	O
with	O
only	O
2	O
registers	B-General_Concept
left	O
.	O
</s>
<s>
In	O
an	O
advanced	O
version	O
of	O
the	O
Sethi	B-Algorithm
–	I-Algorithm
Ullman	I-Algorithm
algorithm	I-Algorithm
,	O
the	O
arithmetic	O
expressions	O
are	O
first	O
transformed	O
,	O
exploiting	O
the	O
algebraic	O
properties	O
of	O
the	O
operators	O
used	O
.	O
</s>
