at.dms.kjc.sir.lowering
Class ThreeAddressCode

java.lang.Object
  extended by at.dms.kjc.sir.lowering.ThreeAddressCode

public class ThreeAddressCode
extends Object

Turn statements into three-address code optionally depending on overridable simpleExpression and shouldConvertExpression methods.
Allowing the user to override shouldConvertExpression forces this class to be non-static.
Notes:

Author:
Allyn Dimock

Field Summary
protected  Set<JExpression> exprsToExpand
          If using ThreeAddressCode to convert just parts of the code you will want to override ThreeAddressExpressionCheck
protected  JStatement[] statementBeingChecked
          Statement on latest call to shouldConvertStatement.
 
Constructor Summary
ThreeAddressCode()
          Constructor, initializes @{link #exprsToExpand}.
 
Method Summary
static List<JStatement> destructureOptBlock(JStatement maybeBlock)
          Return list of statements from a single statement by opening up a block body.
 boolean literalOrVariable(JExpression exp)
          is an expression a literal or a variable?
static String nextTemp()
          nextTemp returns a fresh variable name (hopefully)
protected  boolean shouldConvertExpression(JExpression exp)
          Determine whether to convert an expression to 3-address code.
protected  boolean shouldConvertStatement(JStatement stmt)
          Determine whether to convert a statement to 3-address code.
protected  boolean shouldConvertTopExpression(JExpression expr)
          Determine if should convert expression directly under statement level.
protected  boolean simpleExpression(JExpression exp)
          Simple expression returns true if an expression should shoud not be passed to E[[]].
static JStatement structureOptBlock(List<JStatement> stmts)
          Return a single statement from a list of statements by creating a block if needed.
static JStatement structureOptCompound(List<JStatement> stmts)
          Return a single statement from a list of statements by creating a JCompoundStatement if needed.
 SIRStream threeAddressCode(SIRStream str)
          Turn all filters in stream into three-address code.
 SIRFilter threeAddressCodeFilter(SIRFilter filter)
          Turn a filter into three-address code.
 JMethodDeclaration threeAddressCodeMethod(JMethodDeclaration method, CType inputTapeType)
          Turn a method declaration into three-address code.
 List<JStatement> threeAddressCodeStatement(JStatement stmt, CType inputTapeType)
          Turn a statement into three-address code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

exprsToExpand

protected final Set<JExpression> exprsToExpand
If using ThreeAddressCode to convert just parts of the code you will want to override ThreeAddressExpressionCheck


statementBeingChecked

protected final JStatement[] statementBeingChecked
Statement on latest call to shouldConvertStatement. If you override shouldConvertStatement, keep this up-to-date. Is final, and value is kept as a one-element array in case it is needed in an inner class.

Constructor Detail

ThreeAddressCode

public ThreeAddressCode()
Constructor, initializes @{link #exprsToExpand}.

Method Detail

threeAddressCode

public SIRStream threeAddressCode(SIRStream str)
Turn all filters in stream into three-address code.

Parameters:
str - : stream in which all filters are processed.
Returns:
str for convenience: filters munged in place.

threeAddressCodeFilter

public SIRFilter threeAddressCodeFilter(SIRFilter filter)
Turn a filter into three-address code.

Parameters:
filter - : filter to process.
Returns:
filter for convenience: munged in place.

threeAddressCodeMethod

public JMethodDeclaration threeAddressCodeMethod(JMethodDeclaration method,
                                                 CType inputTapeType)
Turn a method declaration into three-address code.

Parameters:
method - : method to process the body of.
inputTapeType - : type returned by peek or pop.
Returns:
method for convenience: munged in place.

threeAddressCodeStatement

public List<JStatement> threeAddressCodeStatement(JStatement stmt,
                                                  CType inputTapeType)
Turn a statement into three-address code.

Parameters:
stmt - : statement to convert to three-address code
inputTapeType - : type returned by peek or pop.
Returns:
list of statements that the given statement expands into; may share parts of passed statment.

shouldConvertStatement

protected boolean shouldConvertStatement(JStatement stmt)
Determine whether to convert a statement to 3-address code.
Should be overridable for your needs, but you are more likely to override shouldConvertExpression(JExpression). Returns that a statement should be converted if any expression directly under the statement in the AST should be converted. For instance, a "for" statement should be converted if its initialization of check needs conversion; but does not check the sub-statements that are the "for" body or the update, since these could be converted directly. If overriding, make sure that you keep statementBeingChecked[0] up-to-date. One statement that you may want to override: ExpressionListStatement should be converted unless it has a single expression which is simple or should not be converted.

Parameters:
exp - : Statement to check as to whether to convert to 3-address code.
Returns:
true : override this to return what you need.

shouldConvertTopExpression

protected boolean shouldConvertTopExpression(JExpression expr)
Determine if should convert expression directly under statement level. If overriding this statement, you need to pass the expression to a subclass of ThreeAddressExpressionCheck that (1) clears any old contents of @{link #exprsToExpand}, (2) Walks the expression and decides whether there are any expressions that it needs to expand, and (3) adds all such expressions to @{link #exprsToExpand}. ThreeAddressCode will then call another visitor to expand the expressions to three-address code. Note: if you require an expression to be expanded, then you should also require all expressions in its context to be expanded, otherwise ThreeAddressCode may (will) produce incorrect code.

Parameters:
expr - Expression to check for expansion
Returns:
true if the top-level expression or any subexpressions (recursively) need expanding.

shouldConvertExpression

protected boolean shouldConvertExpression(JExpression exp)
Determine whether to convert an expression to 3-address code.
Is overridable for your needs, but you are more likely to just override the shouldConvertTopExpression and leave shouldConvertExpression alone. The default case claims that every expression in exprsToExpand should be converted unless it is an assignment from a simpleExpression() to a simpleExpression().

Parameters:
exp - : Expression to check as to whether to convert to 3-address code.
Returns:
true : override this to return what you need.

simpleExpression

protected boolean simpleExpression(JExpression exp)
Simple expression returns true if an expression should shoud not be passed to E[[]]. I.e. the expression is a literal, variable, name, or a field or array access specified using only a simple expression.

Parameters:
exp - : expression to check
Returns:
true if no need to convert.

literalOrVariable

public boolean literalOrVariable(JExpression exp)
is an expression a literal or a variable?

Parameters:
exp -
Returns:

nextTemp

public static String nextTemp()
nextTemp returns a fresh variable name (hopefully)

Returns:
a fresh name.

destructureOptBlock

public static List<JStatement> destructureOptBlock(JStatement maybeBlock)
Return list of statements from a single statement by opening up a block body. Also works with a JCompoundStatement.
Warning: side effects to statements will show up in original block.

Parameters:
maybeBlock - a block, or other single statement, or null.
Returns:
a list of 0 or more statements.

structureOptBlock

public static JStatement structureOptBlock(List<JStatement> stmts)
Return a single statement from a list of statements by creating a block if needed.
Warning: side effects to statements will show up in created block.

Parameters:
stmts - : a list of statements
Returns:
a single statement.

structureOptCompound

public static JStatement structureOptCompound(List<JStatement> stmts)
Return a single statement from a list of statements by creating a JCompoundStatement if needed.
Warning: side effects to statements will show up in created JCompoundStatement.

Parameters:
stmts - : a list of statements
Returns:
a single statement.