next up previous
Next: Tree Traversal of MIR Up: The MIR Format Previous: Node-subclass prototypes


Details of the MIR Format

The MIR format transforms the AST in the following ways. It converts the initialization of automatic variables into assignment statements at the beginning of the relevant block. Next, all labelNodes are modified so that the stmt field contains a blockNode containing only an empty statement (exprstmtNode with a NULL expression). Finally, the resulting AST is flattened to remove unnecessary nesting of blocks, and all variable declarations are moved up to the procNode level.

The first level of dismantling transforms complex control flow constructs. During this phase, all loops (whileNode, doNode, forNode), if statements (ifNode), and switch statements (switchNode) are replaced with equivalent conditiongotoNodes, gotoNodes, and labelNodes. Additionally, the ternary operator (ternaryNode) and short-circuit conditional operators (&&, ||) are transformed into equivalent ifNodes, which are subsequently transformed as mentioned above.

The second dismantling level transforms expressions into a series of simple expression with intermediate results. Roughly speaking, a simple expression corresponds to an instruction of a modern RISC microprocessor. Thus, simple expressions contain at most one operator (excluding assignments ) and one assignment; operands must be addressable by a single load instruction.

Fully dismantled code only contains five types of statement nodes in the body of a procedure: labelNode, gotoNode, conditiongotoNode, threeAddrNode, and returnNode. This means that for processing the code only five methods for Walkers, Visitors, and Changers are needed to visit these nodes: at_label(), at_goto(), at_conditiongoto(), at_threeAddr(), and at_return(). The argument lists for these methods matches the convention for the other at_*() methods in the relevant class.

In MIR form, the procNode for the procedure has two new fields. The return_label() methods access a field containing a pointer to the labelNode at the beginning of the return block. The declNode for the returned value is accessible using the return_decl() methods.


next up previous
Next: Tree Traversal of MIR Up: The MIR Format Previous: Node-subclass prototypes
Adam C. Brown 2006-01-26