Content deleted Content added
No edit summary |
Adding short description: "Algorithm for minimising register usage" |
||
(10 intermediate revisions by 8 users not shown) | |||
Line 1:
{{Short description|Algorithm for minimising register usage}}
In [[computer science]], the '''Sethi–Ullman algorithm''' is an [[algorithm]] named after [[Ravi Sethi]] and [[Jeffrey D. Ullman]], its inventors, for translating [[abstract syntax tree]]s into [[machine code]] that uses as few [[Processor register|registers]] as possible.
==Overview==
When [[code generation (compiler)|generating code]] for arithmetic expressions, the [[compiler]] has to decide which is the best way to translate the expression in terms of number of instructions used as well as number of registers needed to evaluate a certain subtree. Especially in the case that free registers are scarce, the [[order of evaluation]] can be important to the length of the generated code, because different orderings may lead to larger or smaller numbers of intermediate values being [[register allocation|spilled]] to memory and then restored. The Sethi–Ullman algorithm (also known as '''Sethi–Ullman numbering''')
==Simple Sethi–Ullman algorithm==
Line 8 ⟶ 9:
# Traverse the [[abstract syntax tree]] in pre- or postorder
## For every leaf node, if it is a non-constant
## For every non-leaf node
# To emit code, if the subtrees need different numbers of registers, evaluate the subtree needing the most registers first (since the register needed to save the result of one subtree may make the other one [[Register spilling|spill]]), otherwise the order is irrelevant.
===Example===
For an arithmetic expression <math>a = (b + c + f * g)
=
Line 59:
==References==
*{{citation|title=The Generation of Optimal Code for Arithmetic Expressions|first1=Ravi|last1=Sethi|author1-link=Ravi Sethi|first2=Jeffrey D.|last2=Ullman|author2-link=Jeffrey D. Ullman|journal=[[Journal of the Association for Computing Machinery]]|volume=17|issue=4|year=1970|pages=715–728|doi=10.1145/321607.321620|hdl=10338.dmlcz/101207|hdl-access=free}}.
==External links==
*[
{{DEFAULTSORT:Sethi-Ullman algorithm}}
|