Previous: compiler letrec wsd, Up: compiler letrec [Index]
The SCC algorithm builds a directed graph representation of the
bindings in a single recbind
or rec*bind
structure,
outlining the dependencies among right–hand side expressions; in the
graph each binding is a vertex (also called node). It then applies
Tarjan’s algorithm to the graph and partitions the bindings into
clusters of Strongly Connected Components (SCC).
Here is the gist of the algorithm:
letrec
form:
(letrec ((D (lambda () B)) (C D) (B C) (A B)) ?body)
we build a directed graph of dependencies between the bindings; the graph of dependencies for the given form is:
A --> B --> C ^ | | | D <---
The following picture shows an ongoing visit with path ‘A’, ‘B’, ‘C’, ‘D’; the serial index of each vertex is in square brackets:
A[0] --> B[1] --> C[2] STK == A, B, C, D ^ | . | . | D[3] <----
let’s say we are visiting ‘D’ and considering the successor vertex ‘B’ as next step.
A[0] --> B[1] --> C[2] STK == A, B, C, D ^ | . | . | D[1] <-----
there are no more successor vertexes from ‘D’ so we step back to ‘C’; notice that we leave the stack unchanged.
A[0] --> B[1] --> C[1] STK == A, B, C, D ^ . . . . . D[1] <.....
there are no more successor vertexes from ‘C’ so we step back to ‘B’; notice that we leave the stack unchanged.
STK == A, B, C, D |-------| SCC
so we pop them from the stack and form a cluster with them. The vertexes in a cluster are marked as “done” and will be skipped in further steps of the visit, as if they are not there.
recbind
or rec*bind
structs:
So we can arrange the evaluation as if each cluster comes from a nested binding form; if the returned list is:
((?cluster-binding-1 ...) (?cluster-binding-2 ...) (?cluster-binding-3 ...))
the equivalent nested binding forms are:
(recbind (?cluster-binding-3 ...) (recbind (?cluster-binding-2 ...) (recbind (?cluster-binding-1 ...) ?body)))
(bind ((?complex.lhs '#!void) ...) (fix ((?fixable.lhs ?fixable.rhs) ...) (assign ?complex.lhs ?complex.rhs) ... ?body))
Previous: compiler letrec wsd, Up: compiler letrec [Index]