Package com.tonic.analysis.source.lower
Class LoweringContext
- java.lang.Object
-
- com.tonic.analysis.source.lower.LoweringContext
-
public class LoweringContext extends Object
Shared mutable state for AST-to-IR lowering: variable bindings, the current block, and break/continue targets.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classLoweringContext.LoopTargetsLoop target information for break/continue.
-
Constructor Summary
Constructors Constructor Description LoweringContext(IRMethod irMethod, ConstPool constPool, TypeResolver typeResolver)Creates a lowering context for one method.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearArrayConstructors()Clears array constructors after they have been processed.voidclearSwitchLabels()Clears switch labels (after switch statement processing).voidclearSyntheticMethods()Clears synthetic methods after they have been processed.IRBlockcreateBlock()Creates a basic block and adds it to the method.IRBlockcreateBlock(String prefix)Creates a basic block with a name prefix and adds it to the method.IRTypedeclaredTypeOf(String name)The DECLARED type of a live source local.voiddeclareLocal(String name, IRType type, boolean isParameter)Declares a source-level local, parameter or receiver with its declared type.voiddeclareLocal(String name, IRType type, boolean isParameter, String signature)AsdeclareLocal(String, IRType, boolean)with the declared type's generic signature.StringgenerateArrayConstructorMethodName()Generates a unique name for a synthetic array constructor method, advancing the counter.StringgenerateLambdaMethodName()Generates a unique name for a synthetic lambda method, advancing the counter.intgetArrayConstructorCounter()List<SyntheticArrayConstructor>getArrayConstructors()LoweringContext.LoopTargetsgetBreakFrame(String label)Resolves the loop/switch frame abreaktargets - the labeled frame, else the innermost.IRBlockgetBreakTarget(String label)The block abreakjumps to - the labeled frame's break target, else the innermost frame's.ConstPoolgetConstPool()LoweringContext.LoopTargetsgetContinueFrame(String label)Resolves the loop frame acontinuetargets - the labeled frame, else the innermost frame with a continue-target.IRBlockgetContinueTarget(String label)Gets the continue target for the current or labeled loop.IRBlockgetCurrentBlock()StringgetCurrentMethodName()SourceTypegetCurrentMethodReturnType()Map<String,IRMethod.SourceLocal>getCurrentSourceLocal()IRMethodgetIrMethod()Map<String,LoweringContext.LoopTargets>getLabelMap()intgetLambdaCounter()Deque<LoweringContext.LoopTargets>getLoopStack()intgetNextLocalIndex()intgetOrAllocateLocalIndex(String name)The local slot a variable occupies, allocating the next free one on first use.StringgetOwnerClass()StringgetSuperClassName()IRBlockgetSwitchLabel(String label)Map<String,IRBlock>getSwitchLabelMap()List<SyntheticLambdaMethod>getSyntheticMethods()intgetTempCounter()TypeResolvergetTypeResolver()SSAValuegetVariable(String name)Gets a variable's current SSA value.Map<String,Integer>getVariableLocalIndices()Map<String,SSAValue>getVariableMap()booleanhasVariable(String name)voidinitializeLocalSlots(int parameterSlotCount)Starts body-local allocation after the parameter slots.booleanisEmitLocalInstructions()static StringlambdaEnclosingName(String methodName)The enclosing-method label javac uses in a synthetic lambda name:<init>is "new" and<clinit>is "static".StringnewTempName()Generates a unique temporary variable name, advancing the counter.SSAValuenewValue(IRType type)Creates a new SSA value.SourceTypepeekExpectedType()voidpopExpectedType()Pops the innermost expected type.voidpopLoop()Pops the current loop targets from the stack.voidpushExpectedType(SourceType type)Pushes the type the surrounding declaration expects of the expression being lowered.voidpushLoop(String label, IRBlock continueTarget, IRBlock breakTarget, int finallyDepth)Pushes loop targets onto the stack.voidregisterArrayConstructor(SyntheticArrayConstructor constructor)Registers a synthetic array constructor for later generation.voidregisterParameter(String name, int localIndex, SSAValue value)Registers a parameter with its local slot index without emitting StoreLocal.voidregisterSyntheticMethod(SyntheticLambdaMethod method)Registers a synthetic lambda method for later generation.voidrestoreVariables(Map<String,SSAValue> snapshot)Restores variable state from a snapshot, discarding the current bindings.voidsetCurrentBlock(IRBlock currentBlock)Sets the block instructions are emitted into.voidsetCurrentMethodName(String currentMethodName)Sets the name of the method being lowered.voidsetCurrentMethodReturnType(SourceType type)Sets the declared return type of the method being lowered.voidsetEmitLocalInstructions(boolean emitLocalInstructions)Sets whether Load/Store instructions are emitted for variable access; loops need them.voidsetOwnerClass(String ownerClass)Sets the owner class of the current method.voidsetSuperClassName(String superClassName)Sets the superclass of the owner class.voidsetSwitchLabel(String label, IRBlock target)Registers the block a switch case branches to.voidsetVariable(String name, SSAValue value)Sets or updates a variable's SSA value.Map<String,SSAValue>snapshotVariables()Copies the current variable bindings so a branch can be lowered and then rolled back.
-
-
-
Constructor Detail
-
LoweringContext
public LoweringContext(IRMethod irMethod, ConstPool constPool, TypeResolver typeResolver)
Creates a lowering context for one method.- Parameters:
irMethod- the IR method being builtconstPool- the constant pool receiving new entriestypeResolver- the resolver for field and method types
-
-
Method Detail
-
getIrMethod
public IRMethod getIrMethod()
- Returns:
- the IR method being built
-
getConstPool
public ConstPool getConstPool()
- Returns:
- the constant pool for creating constants and references
-
getTypeResolver
public TypeResolver getTypeResolver()
- Returns:
- the resolver for looking up field and method types from the ClassPool
-
getCurrentBlock
public IRBlock getCurrentBlock()
- Returns:
- the block instructions are currently emitted into
-
setCurrentBlock
public void setCurrentBlock(IRBlock currentBlock)
Sets the block instructions are emitted into.- Parameters:
currentBlock- the new emission target
-
getVariableMap
public Map<String,SSAValue> getVariableMap()
- Returns:
- the map from variable names to their current SSA values
-
getVariableLocalIndices
public Map<String,Integer> getVariableLocalIndices()
- Returns:
- the map from variable names to their local slot indices
-
getCurrentSourceLocal
public Map<String,IRMethod.SourceLocal> getCurrentSourceLocal()
- Returns:
- the in-scope source-local record per name (re-created on each declaration, so disjoint same-name declarations stay distinct), used to capture LocalVariableTable info during lowering
-
getNextLocalIndex
public int getNextLocalIndex()
- Returns:
- the next available local slot index
-
isEmitLocalInstructions
public boolean isEmitLocalInstructions()
- Returns:
- whether Load/Store instructions are emitted for variables (needed for loops)
-
setEmitLocalInstructions
public void setEmitLocalInstructions(boolean emitLocalInstructions)
Sets whether Load/Store instructions are emitted for variable access; loops need them.- Parameters:
emitLocalInstructions- true to emit explicit local loads and stores
-
getLoopStack
public Deque<LoweringContext.LoopTargets> getLoopStack()
- Returns:
- the stack of loop targets for break/continue
-
getLabelMap
public Map<String,LoweringContext.LoopTargets> getLabelMap()
- Returns:
- the map from labels to their loop targets
-
getSwitchLabelMap
public Map<String,IRBlock> getSwitchLabelMap()
- Returns:
- the map from switch labels to their target blocks
-
getTempCounter
public int getTempCounter()
- Returns:
- the counter for generating temporary variable names
-
getLambdaCounter
public int getLambdaCounter()
- Returns:
- the counter for generating unique lambda method names
-
getArrayConstructorCounter
public int getArrayConstructorCounter()
- Returns:
- the counter for generating unique array constructor method names
-
getCurrentMethodName
public String getCurrentMethodName()
- Returns:
- the name of the method being lowered
-
setCurrentMethodName
public void setCurrentMethodName(String currentMethodName)
Sets the name of the method being lowered.- Parameters:
currentMethodName- the method name
-
getCurrentMethodReturnType
public SourceType getCurrentMethodReturnType()
- Returns:
- the current method return type
-
setCurrentMethodReturnType
public void setCurrentMethodReturnType(SourceType type)
Sets the declared return type of the method being lowered.- Parameters:
type- the target type of the method's return values
-
pushExpectedType
public void pushExpectedType(SourceType type)
Pushes the type the surrounding declaration expects of the expression being lowered.- Parameters:
type- the expected type
-
popExpectedType
public void popExpectedType()
Pops the innermost expected type.
-
peekExpectedType
public SourceType peekExpectedType()
- Returns:
- the innermost expected type, or null when none is active
-
getOwnerClass
public String getOwnerClass()
- Returns:
- the owner class of the current method
-
setOwnerClass
public void setOwnerClass(String ownerClass)
Sets the owner class of the current method.- Parameters:
ownerClass- the internal name of the owning class
-
getSuperClassName
public String getSuperClassName()
- Returns:
- the superclass of the owner class
-
setSuperClassName
public void setSuperClassName(String superClassName)
Sets the superclass of the owner class.- Parameters:
superClassName- the internal name of the superclass
-
initializeLocalSlots
public void initializeLocalSlots(int parameterSlotCount)
Starts body-local allocation after the parameter slots.- Parameters:
parameterSlotCount- slots the receiver and parameters occupy
-
registerParameter
public void registerParameter(String name, int localIndex, SSAValue value)
Registers a parameter with its local slot index without emitting StoreLocal.- Parameters:
name- parameter namelocalIndex- slot the parameter arrives invalue- SSA value holding the incoming argument
-
declareLocal
public void declareLocal(String name, IRType type, boolean isParameter)
Declares a source-level local, parameter or receiver with its declared type.- Parameters:
name- source name of the localtype- declared typeisParameter- true when the local is a parameter or the receiver
-
declareLocal
public void declareLocal(String name, IRType type, boolean isParameter, String signature)
AsdeclareLocal(String, IRType, boolean)with the declared type's generic signature.- Parameters:
name- source name of the localtype- declared typeisParameter- true when the local is a parameter or the receiversignature- generic signature of the declared type, or null when it has none
-
declaredTypeOf
public IRType declaredTypeOf(String name)
The DECLARED type of a live source local.- Parameters:
name- variable to look up- Returns:
- the declared type, or null when the variable was never declared
-
getOrAllocateLocalIndex
public int getOrAllocateLocalIndex(String name)
The local slot a variable occupies, allocating the next free one on first use.- Parameters:
name- variable to place- Returns:
- the slot index bound to the variable
-
createBlock
public IRBlock createBlock()
Creates a basic block and adds it to the method.- Returns:
- the new block
-
createBlock
public IRBlock createBlock(String prefix)
Creates a basic block with a name prefix and adds it to the method.- Parameters:
prefix- prefix for the block's generated name- Returns:
- the new block
-
setVariable
public void setVariable(String name, SSAValue value)
Sets or updates a variable's SSA value.- Parameters:
name- variable being assignedvalue- value assigned to it
-
getVariable
public SSAValue getVariable(String name)
Gets a variable's current SSA value.- Parameters:
name- variable to read- Returns:
- the bound value, or the value the emitted load produces
- Throws:
LoweringException- if the name is not bound
-
hasVariable
public boolean hasVariable(String name)
- Parameters:
name- variable name to test- Returns:
- true if the name is bound to a value
-
newValue
public SSAValue newValue(IRType type)
Creates a new SSA value.- Parameters:
type- IR type of the value- Returns:
- the new value
-
newTempName
public String newTempName()
Generates a unique temporary variable name, advancing the counter.- Returns:
- the generated name
-
pushLoop
public void pushLoop(String label, IRBlock continueTarget, IRBlock breakTarget, int finallyDepth)
Pushes loop targets onto the stack.- Parameters:
label- optional label for labeled loopscontinueTarget- block to jump to for continuebreakTarget- block to jump to for breakfinallyDepth- finally-stack size at the moment the loop is entered
-
popLoop
public void popLoop()
Pops the current loop targets from the stack.
-
getContinueTarget
public IRBlock getContinueTarget(String label)
Gets the continue target for the current or labeled loop.- Parameters:
label- loop label, or null for the innermost enclosing loop- Returns:
- the block the continue jumps to
- Throws:
LoweringException- if the label is unknown or no enclosing loop is open
-
getBreakTarget
public IRBlock getBreakTarget(String label)
The block abreakjumps to - the labeled frame's break target, else the innermost frame's.- Parameters:
label- loop or switch label, or null for the innermost frame- Returns:
- the block the break jumps to
- Throws:
LoweringException- if the label is unknown or no frame is open
-
getBreakFrame
public LoweringContext.LoopTargets getBreakFrame(String label)
Resolves the loop/switch frame abreaktargets - the labeled frame, else the innermost.- Parameters:
label- loop or switch label, or null for the innermost frame- Returns:
- the frame the break leaves
- Throws:
LoweringException- if the label is unknown or no frame is open
-
getContinueFrame
public LoweringContext.LoopTargets getContinueFrame(String label)
Resolves the loop frame acontinuetargets - the labeled frame, else the innermost frame with a continue-target.- Parameters:
label- loop label, or null for the innermost enclosing loop- Returns:
- the frame the continue leaves to
- Throws:
LoweringException- if the label is unknown or no enclosing loop is open
-
setSwitchLabel
public void setSwitchLabel(String label, IRBlock target)
Registers the block a switch case branches to.- Parameters:
label- case labeltarget- block the case branches to
-
getSwitchLabel
public IRBlock getSwitchLabel(String label)
- Parameters:
label- case label to look up- Returns:
- the block the case branches to, or null when no target is registered
-
clearSwitchLabels
public void clearSwitchLabels()
Clears switch labels (after switch statement processing).
-
snapshotVariables
public Map<String,SSAValue> snapshotVariables()
Copies the current variable bindings so a branch can be lowered and then rolled back.- Returns:
- a detached copy of the name-to-value bindings
-
restoreVariables
public void restoreVariables(Map<String,SSAValue> snapshot)
Restores variable state from a snapshot, discarding the current bindings.- Parameters:
snapshot- bindings taken by snapshotVariables
-
generateLambdaMethodName
public String generateLambdaMethodName()
Generates a unique name for a synthetic lambda method, advancing the counter.- Returns:
- the generated method name
-
lambdaEnclosingName
public static String lambdaEnclosingName(String methodName)
The enclosing-method label javac uses in a synthetic lambda name:<init>is "new" and<clinit>is "static".- Parameters:
methodName- name of the enclosing method- Returns:
- the label to embed in the lambda method name
-
registerSyntheticMethod
public void registerSyntheticMethod(SyntheticLambdaMethod method)
Registers a synthetic lambda method for later generation.- Parameters:
method- lambda body to generate
-
getSyntheticMethods
public List<SyntheticLambdaMethod> getSyntheticMethods()
- Returns:
- a copy of the registered synthetic lambda methods
-
clearSyntheticMethods
public void clearSyntheticMethods()
Clears synthetic methods after they have been processed.
-
generateArrayConstructorMethodName
public String generateArrayConstructorMethodName()
Generates a unique name for a synthetic array constructor method, advancing the counter.- Returns:
- the generated method name
-
registerArrayConstructor
public void registerArrayConstructor(SyntheticArrayConstructor constructor)
Registers a synthetic array constructor for later generation.- Parameters:
constructor- array constructor to generate
-
getArrayConstructors
public List<SyntheticArrayConstructor> getArrayConstructors()
- Returns:
- a copy of the registered array constructors
-
clearArrayConstructors
public void clearArrayConstructors()
Clears array constructors after they have been processed.
-
-