HOME

TheInfoList



OR:

In
computer science Computer science is the study of computation, information, and automation. Computer science spans Theoretical computer science, theoretical disciplines (such as algorithms, theory of computation, and information theory) to Applied science, ...
, the Sethi–Ullman algorithm is an
algorithm In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
named after Ravi Sethi and Jeffrey D. Ullman, its inventors, for translating
abstract syntax tree An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal ...
s into
machine code In computer programming, machine code is computer code consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). For conventional binary computers, machine code is the binaryOn nonb ...
that uses as few registers as possible.


Overview

When generating code for arithmetic expressions, the
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
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 spilled to memory and then restored. The Sethi–Ullman algorithm (also known as Sethi–Ullman numbering) produces code which needs the fewest instructions possible as well as the fewest storage references (under the assumption that at the most
commutativity In mathematics, a binary operation is commutative if changing the order of the operands does not change the result. It is a fundamental property of many binary operations, and many mathematical proofs depend on it. Perhaps most familiar as a p ...
and
associativity In mathematics, the associative property is a property of some binary operations that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a Validity (logic), valid rule of replaceme ...
apply to the operators used, but distributive laws i.e. a * b + a * c = a * (b + c) do not hold). The algorithm succeeds as well if neither
commutativity In mathematics, a binary operation is commutative if changing the order of the operands does not change the result. It is a fundamental property of many binary operations, and many mathematical proofs depend on it. Perhaps most familiar as a p ...
nor
associativity In mathematics, the associative property is a property of some binary operations that rearranging the parentheses in an expression will not change the result. In propositional logic, associativity is a Validity (logic), valid rule of replaceme ...
hold for the expressions used, and therefore arithmetic transformations can not be applied. The algorithm also does not take advantage of common subexpressions or apply directly to expressions represented as general directed acyclic graphs rather than trees.


Simple Sethi–Ullman algorithm

The simple Sethi–Ullman algorithm works as follows (for a load/store architecture): # Traverse the
abstract syntax tree An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal ...
in pre- or postorder ## For every leaf node, if it is a non-constant left-child, assign a 1 (i.e. 1 register is needed to hold the variable/field/etc.), otherwise assign a 0 (it is a non-constant right child or constant leaf node (RHS of an operation – literals, values)). ## For every non-leaf node, if the left and right subtrees respectively need different numbers of registers ''l'' and ''r'', then assign max(''l'', ''r''), otherwise assign ''r'' + 1. # 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 spill), otherwise the order is irrelevant.


Example

For an arithmetic expression a = (b + c + f * g)*(d+3), the
abstract syntax tree An abstract syntax tree (AST) is a data structure used in computer science to represent the structure of a program or code snippet. It is a tree representation of the abstract syntactic structure of text (often source code) written in a formal ...
looks like this: = / \ a * / \ / \ + + / \ / \ / \ d 3 + * / \ / \ b c f g To continue with the algorithm, we need only to examine the arithmetic expression (b + c + f * g) * (d + 3), i.e. we only have to look at the right subtree of the assignment '=': * / \ / \ + + / \ / \ / \ d 3 + * / \ / \ b c f g Now we start traversing the tree (in preorder for now), assigning the number of registers needed to evaluate each subtree (note that the last summand in the expression (b + c + f * g) * (d + 3) is a constant): *2 / \ / \ +2 +1 / \ / \ / \ d1 30 +1 *1 / \ / \ b1 c0f1 g0 From this tree it can be seen that we need 2 registers to compute the left subtree of the '*', but only 1 register to compute the right subtree. Nodes 'c' and 'g' do not need registers for the following reasons: If T is a tree leaf, then the number of registers to evaluate T is either 1 or 0 depending whether T is a left or a right subtree (since an operation such as add R1, A can handle the right component A directly without storing it into a register). Therefore we shall start to emit code for the left subtree first, because we might run into the situation that we only have 2 registers left to compute the whole expression. If we now computed the right subtree first (which needs only 1 register), we would then need a register to hold the result of the right subtree while computing the left subtree (which would still need 2 registers), therefore needing 3 registers concurrently. Computing the left subtree first needs 2 registers, but the result can be stored in 1, and since the right subtree needs only 1 register to compute, the evaluation of the expression can do with only 2 registers left.


Advanced Sethi–Ullman algorithm

In an advanced version of the Sethi–Ullman algorithm, the arithmetic expressions are first transformed, exploiting the algebraic properties of the operators used.


See also

*
Strahler number In mathematics, the Strahler number or Horton–Strahler number of a mathematical tree (graph theory), tree is a numerical measure of its branching complexity. These numbers were first developed in hydrology, as a way of measuring the complexity ...
, the minimum number of registers needed to evaluate an expression without any external storage * Ershov Number, basically the same concept as Strahler number


References

*.


External links


Code Generation for Trees
{{DEFAULTSORT:Sethi-Ullman algorithm Compiler construction Graph algorithms