piglet.translate
Class SpigletContext

java.lang.Object
  extended by piglet.translate.SpigletContext

public final class SpigletContext
extends java.lang.Object

This class represents a context in a given step of translation between Piglet and Spiglet. It stores a context node where statements are attached (as well as helper functions to attach different kinds of statements), a mapping of Labels and Temps (from Piglet Labels and Temps), and the number of parameter Temps (when translating a Procedure) that aren't supposed to be mapped.

Author:
Santoso Wijaya

Constructor Summary
SpigletContext(Node node)
          Creates a SpigletContext with the given context node and an empty variable mapping.
SpigletContext(Node node, java.util.Map<java.lang.String,SpigletTemp> varMap)
          Creates a SpigletContext with the given context node and pre-made variable mapping.
 
Method Summary
 void attach(Node statement)
          Attaches a Stmt (possibly a (Label)? Stmt sequence) node to the context node, which in pre-condition assumed to be a StmtList node or a NodeListOptional node.
 void attachCJumpStmt(SpigletTemp cond, SpigletLabel alt)
          Creates a CJumpStmt node with the given Temp as the conditional expression, and the given Label as the false branch destination, and then attaches the node to the context.
 void attachErrorStmt()
          Creates an attaches a ErrorStmt node to the context.
 void attachHLoadStmt(SpigletTemp dest, SpigletTemp addr, IntegerLiteral offset)
          Creates a HLoadStmt node with the given Temps (referring to where to store the data and where the address pointer is stored, respectively) and offset node, then attaches it to the context.
 void attachHStoreStmt(SpigletTemp addr, IntegerLiteral offset, SpigletTemp val)
          Creates a HStoreStmt node with the given Temps (referring to the address to which to store the data and the the data itself to be stored, respectively) and offset node, then attaches it to the contex.
 void attachJumpStmt(SpigletLabel label)
          Creates a JumpStmt node with the given Label argument, then attaches it as a Stmt node to the context.
 void attachLabeledNoOpStmt(SpigletLabel label)
          Creates a NoOpStmt node sequenced with a Label node with the given label value, then attaches it as a Stmt node to the context
 void attachLabeledStmt(SpigletLabel label, Stmt statement)
          Creates a node sequence of Label and Stmt to be attached to the context.
 void attachMoveStmt(SpigletTemp temp, Exp expr)
          Creates a MoveStmt node with the given Temp and expression subtree, then attaches it as a Stmt node to the context.
 void attachProcedure(Procedure proc)
          Attaches a Procedure node to the context, which is pre-conditioned to be a NodeListOptional node.
 NodeListOptional getNodeListContext()
          Returns the context node as a NodeListOptional node.
 StmtList getStmtListContext()
          Returns the context node as a StmtList node.
 SpigletTemp map(java.lang.String var)
          Maps a variable to a Spiglet temporary.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpigletContext

public SpigletContext(Node node,
                      java.util.Map<java.lang.String,SpigletTemp> varMap)
Creates a SpigletContext with the given context node and pre-made variable mapping.

Parameters:
node - the context
varMap - the mapping of variables to Spiglet temporaries

SpigletContext

public SpigletContext(Node node)
Creates a SpigletContext with the given context node and an empty variable mapping.

Parameters:
node - the context
Method Detail

getStmtListContext

public StmtList getStmtListContext()
Returns the context node as a StmtList node. Pre-condition is assumed.

Returns:
the context node as a StmtList node

getNodeListContext

public NodeListOptional getNodeListContext()
Returns the context node as a NodeListOptional node. Pre-condition is assumed.

Returns:
the context node as a NodeListOptional node

map

public SpigletTemp map(java.lang.String var)
Maps a variable to a Spiglet temporary. If no such mapping exists, create a new Spiglet Temp object and create a new mapping.

Parameters:
var - the variable to look up
Returns:
the mapped Spiglet temporary

attach

public void attach(Node statement)
Attaches a Stmt (possibly a (Label)? Stmt sequence) node to the context node, which in pre-condition assumed to be a StmtList node or a NodeListOptional node.

Parameters:
statement - the statement subtree

attachLabeledStmt

public void attachLabeledStmt(SpigletLabel label,
                              Stmt statement)
Creates a node sequence of Label and Stmt to be attached to the context.

Parameters:
label - the label
statement - the labeled statement

attachLabeledNoOpStmt

public void attachLabeledNoOpStmt(SpigletLabel label)
Creates a NoOpStmt node sequenced with a Label node with the given label value, then attaches it as a Stmt node to the context

Parameters:
label - the label

attachErrorStmt

public void attachErrorStmt()
Creates an attaches a ErrorStmt node to the context.


attachJumpStmt

public void attachJumpStmt(SpigletLabel label)
Creates a JumpStmt node with the given Label argument, then attaches it as a Stmt node to the context.

Parameters:
label - the label

attachMoveStmt

public void attachMoveStmt(SpigletTemp temp,
                           Exp expr)
Creates a MoveStmt node with the given Temp and expression subtree, then attaches it as a Stmt node to the context.

Parameters:
temp - the Temp to assign the expression to
expr - the expression subtree

attachCJumpStmt

public void attachCJumpStmt(SpigletTemp cond,
                            SpigletLabel alt)
Creates a CJumpStmt node with the given Temp as the conditional expression, and the given Label as the false branch destination, and then attaches the node to the context.

Parameters:
cond - the Temp where the evaluated conditional expression is stored
alt - the Label of the false branch

attachHLoadStmt

public void attachHLoadStmt(SpigletTemp dest,
                            SpigletTemp addr,
                            IntegerLiteral offset)
Creates a HLoadStmt node with the given Temps (referring to where to store the data and where the address pointer is stored, respectively) and offset node, then attaches it to the context.

Parameters:
dest - the Temp to which the loaded data is to be stored
addr - the Temp that contains the address value
offset - contains the byte value of the offset into the address

attachHStoreStmt

public void attachHStoreStmt(SpigletTemp addr,
                             IntegerLiteral offset,
                             SpigletTemp val)
Creates a HStoreStmt node with the given Temps (referring to the address to which to store the data and the the data itself to be stored, respectively) and offset node, then attaches it to the contex.

Parameters:
addr - the Temp that contains the address value
offset - contains the byte value of the offset into the address
val - the Temp that contains the data to be stored

attachProcedure

public void attachProcedure(Procedure proc)
Attaches a Procedure node to the context, which is pre-conditioned to be a NodeListOptional node.

Parameters:
proc - the procedure