Many SUIF programs need to duplicate parts of abstract syntax trees or expression trees. For example, loop transformations such as unrolling and peeling require copies of the loop bodies, and entire procedures must be copied to implement procedure inlining. Because code replication can be quite complicated in SUIF, it is handled in the library by the cloning methods.
Several different kinds of SUIF objects can be cloned. The
tree_node
, tree_node_list
, instruction
, and
operand
classes have clone
methods. The various derived
tree_node
classes also have wrapper methods to cast the result of
the base class clone
method to the appropriate pointer types.
When an object is cloned, everything within it is recursively cloned.
In addition to the child nodes and instructions, this includes symbol
tables and the types and symbols within them. All of the known
references to cloned objects (including those in registered annotations)
are updated.
The complexity of cloning is primarily due to SUIF's scope rules. The code being copied may reference symbols and types that are not visible in the scope where the copy will be used. Thus the cloning process is divided into three steps:
Each of these steps is described in more detail below. The methods that perform the individual steps are available to users who need more control over the cloning process. For example, you may want to insert code to initialize new variables that are created in the destination scope due to exposed references. The cloning methods can also be used to replace references to particular symbols or types without actually copying anything.
The replacements
structure is used throughout the cloning process
to keep track of exposed references and objects that have been replaced.
This structure includes lists of symbols, types, variable definitions,
symbol tables, and instructions. For each kind of object, there are two
lists. Except in intermediate steps, these lists should be the same
length. An entry on the first list refers to an object in the original
code and the corresponding entry on the second list is a new object that
has been created to replace the original.