spiglet.translate
Class KangaContext

java.lang.Object
  extended by spiglet.translate.KangaContext

public final class KangaContext
extends java.lang.Object

This class represents a context in a given step of translation between Spiglet and Kanga. It stores a context node where statements are attached (as well as helper functions to attach different kinds of statements).

Author:
Santoso Wijaya

Field Summary
 int sp
          The stack pointer.
 java.util.Map<java.lang.String,KangaStorage> varMap
          The mapping between a Spiglet temporary (represented by the temporary's ordinal number in string) and its storage in a Kanga construct.
 
Constructor Summary
KangaContext(Node node, java.util.Map<java.lang.String,KangaStorage> storageMap)
          Create a context with the given context node, a premade mapping of variables to Kanga storage (registers, really).
 
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 attachALoadStmt(KangaReg reg, int spillOffset)
          Creates a ALoadStmt node with the given spill offset and register to where loaded data is to be stored, then attaches the statement node to the context.
 void attachAStoreStmt(int spillOffset, KangaReg reg)
          Creates a AStoreStmt node with the given spill offset and register where the data to be stored is, then attaches the node to the context.
 void attachCallStmt(SimpleExp expr)
          Creates a SimpleExp node with the given expression, then attaches it to the context.
 void attachCJumpStmt(KangaReg cond, KangaLabel alt)
          Creates a CJumpStmt node with the given Reg 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(KangaReg dest, KangaReg addr, IntegerLiteral offset)
          Creates a HLoadStmt node with the given Regs (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(KangaReg addr, IntegerLiteral offset, KangaReg val)
          Creates a HStoreStmt node with the given Regs (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 context.
 void attachJumpStmt(KangaLabel label)
          Creates a JumpStmt node with the given Label argument, then attaches it as a Stmt node to the context.
 void attachLabeledNoOpStmt(KangaLabel 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(KangaLabel label, Stmt statement)
          Creates a node sequence of Label and Stmt to be attached to the context.
 void attachMoveStmt(KangaReg reg, Exp expr)
          Creates a MoveStmt node with the given Reg and expression subtree, then attaches it as a Stmt node to the context.
 void attachPassArgStmt(int offset, KangaReg reg)
          Creates a PassArgStmt node with the given offset and register, then attaches it to the context.
 void attachProcedure(Procedure proc)
          Attaches a Procedure node to the context, which is pre-conditioned to be a NodeListOptional node.
 int getMaxCallArgs()
          Returns the maximum number of arguments of a call in the body of this context.
 NodeListOptional getNodeListContext()
          Returns the context node as a NodeListOptional node.
 StmtList getStmtListContext()
          Returns the context node as a StmtList node.
 void offerMaxCallArgs(int args)
          Sets the context's maxCallArgs, the maximum number of arguments of a call in the body of this context, if the offered number is larger than it is currently.
 void restoreCalleeRegisters()
          In this context, generate code that pops the values stored on the stack back to callee-saved registers that were previously saved.
 void restoreCallerRegisters()
          In this context, generate code that pops the values stored on the stack back to caller-saved registers that were previously saved.
 void saveCaleeRegisters()
          Given the mapping of variables and kanga storage constructs (registers) in this context, generate code that pushes the values stored in callee-saved registers in the mapping onto the stack.
 void saveCalerRegisters()
          Given the mapping of variables and kanga storage constructs (registers) in this context, generate code that pushes the values stored in caller-saved registers in the mapping onto the stack.
 int spillReg(KangaReg reg)
          Injects Kanga code that pushes the value stored in the given register on to the stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

varMap

public java.util.Map<java.lang.String,KangaStorage> varMap
The mapping between a Spiglet temporary (represented by the temporary's ordinal number in string) and its storage in a Kanga construct.


sp

public int sp
The stack pointer. Always points to the next available offset on the SpilledArg Kanga stack in the current frame.

Constructor Detail

KangaContext

public KangaContext(Node node,
                    java.util.Map<java.lang.String,KangaStorage> storageMap)
Create a context with the given context node, a premade mapping of variables to Kanga storage (registers, really). Stack pointer is initialized to zero. Max call arguments is also initialized to zero.

Parameters:
storageMap - the mapping of variables to Kanga storage construct
node - the context
Method Detail

spillReg

public int spillReg(KangaReg reg)
Injects Kanga code that pushes the value stored in the given register on to the stack. As another side effect of this operation, the stack pointer is advanced. Returns the offset of the stack where said value is stored.

Parameters:
reg - the register where the value to be spilled is
Returns:
the offset of the stack where the value is stored

offerMaxCallArgs

public void offerMaxCallArgs(int args)
Sets the context's maxCallArgs, the maximum number of arguments of a call in the body of this context, if the offered number is larger than it is currently.

Parameters:
args - the offered number of maximum call arguments

getMaxCallArgs

public int getMaxCallArgs()
Returns the maximum number of arguments of a call in the body of this context.

Returns:
the maximum arguments of a call in the body of this context

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

saveCaleeRegisters

public void saveCaleeRegisters()
Given the mapping of variables and kanga storage constructs (registers) in this context, generate code that pushes the values stored in callee-saved registers in the mapping onto the stack.


restoreCalleeRegisters

public void restoreCalleeRegisters()
In this context, generate code that pops the values stored on the stack back to callee-saved registers that were previously saved.


saveCalerRegisters

public void saveCalerRegisters()
Given the mapping of variables and kanga storage constructs (registers) in this context, generate code that pushes the values stored in caller-saved registers in the mapping onto the stack.


restoreCallerRegisters

public void restoreCallerRegisters()
In this context, generate code that pops the values stored on the stack back to caller-saved registers that were previously saved.


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(KangaLabel 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(KangaLabel 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(KangaLabel 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(KangaReg reg,
                           Exp expr)
Creates a MoveStmt node with the given Reg and expression subtree, then attaches it as a Stmt node to the context.

Parameters:
reg - the Reg to assign the expression to
expr - the expression subtree

attachCJumpStmt

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

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

attachHLoadStmt

public void attachHLoadStmt(KangaReg dest,
                            KangaReg addr,
                            IntegerLiteral offset)
Creates a HLoadStmt node with the given Regs (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 Reg to which the loaded data is to be stored
addr - the Reg that contains the address value
offset - contains the byte value of the offset into the address

attachHStoreStmt

public void attachHStoreStmt(KangaReg addr,
                             IntegerLiteral offset,
                             KangaReg val)
Creates a HStoreStmt node with the given Regs (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 context.

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

attachAStoreStmt

public void attachAStoreStmt(int spillOffset,
                             KangaReg reg)
Creates a AStoreStmt node with the given spill offset and register where the data to be stored is, then attaches the node to the context.

Parameters:
spillOffset - the spill offset where the data is to be stored
reg - the register where the data is

attachALoadStmt

public void attachALoadStmt(KangaReg reg,
                            int spillOffset)
Creates a ALoadStmt node with the given spill offset and register to where loaded data is to be stored, then attaches the statement node to the context.

Parameters:
reg - the register to where loaded data is stored
spillOffset - the offset of the stack where the data is

attachCallStmt

public void attachCallStmt(SimpleExp expr)
Creates a SimpleExp node with the given expression, then attaches it to the context.

Parameters:
expr - the expression subtree

attachPassArgStmt

public void attachPassArgStmt(int offset,
                              KangaReg reg)
Creates a PassArgStmt node with the given offset and register, then attaches it to the context.

Parameters:
offset - the offset of the spilled argument stack
reg - the register from which the argument is spilled

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