Class RecoveryContext
- java.lang.Object
-
- com.tonic.analysis.source.recovery.RecoveryContext
-
public class RecoveryContext extends Object
Holds shared state during expression recovery.
-
-
Constructor Summary
Constructors Constructor Description RecoveryContext(IRMethod irMethod, MethodEntry sourceMethod, DefUseChains defUseChains)Creates an empty context, deriving the local model from the IR method.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbaselineDeclaredVariables()Snapshots the current declared set as the method's baseline (the pre-declared parameters), so a full re-pass can restore it.voidcacheExpression(SSAValue value, Expression expr)Stores the recovered expression for a value, replacing any earlier one.voidclearDeclaredVariables()Clears declared variables (for new scope).StringconsumePendingNew(SSAValue value)Removes the pending new record for a value and returns its class name.StringconsumePendingNewLocalSlot(int localIndex)Removes the pending new record for a local slot and returns its class name.StringdebugDescriptorAt(int slot, int offset)The LocalVariableTable type descriptor a slot carries at a bytecode offset.StringdebugDescriptorAtStore(int slot, int storeOffset)The LVT descriptor at the pc where a store takes effect.StringdebugNameForSlot(int slot)The recovered debug name a slot carries.Deque<Set<String>>getBranchScopedVariables()ExpressiongetCachedExpression(SSAValue value)SourceTypegetDeclaredType(String name)Set<String>getDeclaredVariables()Map<String,SourceType>getDeclaredVariableTypes()DefUseChainsgetDefUseChains()Deque<Set<String>>getForLoopScopedVariables()Set<SSAValue>getInlinedValues()IRMethodgetIrMethod()MethodLocalsgetLocals()StringgetLocalSlotName(int slotIndex)Map<Integer,String>getLocalSlotNames()Set<SSAValue>getMaterializedValues()Map<SSAValue,String>getPendingNewInstructions()Map<Integer,String>getPendingNewLocalSlots()Set<SSAValue>getRecordDeconstructionTemps()Map<SSAValue,Expression>getRecoveredExpressions()SlotVariablePartitiongetSlotPartition()MethodEntrygetSourceMethod()Map<SSAValue,Integer>getSsaValueSlot()intgetSSAValueSlot(SSAValue value)intgetSyntheticCounter()StringgetVariableName(SSAValue value)Map<SSAValue,String>getVariableNames()booleanisDeclared(String name)booleanisInBranchScope()booleanisInlined(SSAValue value)booleanisMaterialized(SSAValue value)booleanisParameterOrThisSlot(int slot)booleanisPendingNew(SSAValue value)booleanisPinnedToVariable(SSAValue value)booleanisRecovered(SSAValue value)voidmarkDeclared(String name)Marks a variable name as declared.voidmarkDeclaredInBranch(String name)Marks a variable as declared in the current scope.voidmarkDeclaredInForLoopInit(String name)Marks a variable as declared in a for-loop init expression.voidmarkDeclaredWithType(String name, SourceType type)Marks a variable name as declared, recording its type when one is given.voidmarkInlined(SSAValue value)Records that a value's expression was folded into its single use rather than assigned.voidmarkMaterialized(SSAValue value)Marks an SSA value as materialized into a variable.intnextSyntheticId()intparameterIndexForSlot(int slot)The zero-based parameter index of a parameter slot, with the receiver excluded from the numbering.intparameterSlot(SSAValue value)voidpinToVariable(SSAValue value)Pins a value to its variable.voidpopBranchScope()Pops the current branch scope and removes its variables from declaredVariables.voidpopForLoopScope()Pops the current for-loop scope and removes its variables from declaredVariables.voidpushBranchScope()Pushes a new scope for if-then-else branch variables.voidpushForLoopScope()Pushes a new scope for for-loop variables.voidregisterPendingNew(SSAValue result, String className)Registers an allocation result that awaits its<init>call.voidregisterPendingNewLocalSlot(int localIndex, String className)Registers a local slot as containing a pending new value.voidresetDeclaredVariablesToBaseline()Restores the declared set to the method baseline captured bybaselineDeclaredVariables().voidsetDebugDescriptorResolver(BiFunction<Integer,Integer,String> resolver)Installs the lookup backing debugDescriptorAt.voidsetDebugNameResolver(IntFunction<String> resolver)Installs the lookup backing debugNameForSlot.voidsetDebugStoreDescriptorResolver(BiFunction<Integer,Integer,String> resolver)Installs the lookup backing debugDescriptorAtStore.voidsetLocalSlotName(int slotIndex, String name)Binds a variable name to a local slot so later loads from the slot recover the same name.voidsetSlotPartition(SlotVariablePartition slotPartition)voidsetSSAValueSlot(SSAValue value, int slotIndex)Records the slot an SSA value was stored to, keeping the first one recorded.voidsetVariableName(SSAValue value, String name)Binds a name to a value, refusing to rename a parameter that already has one.voidunmarkMaterialized(SSAValue value)Unmarks an SSA value as materialized, allowing it to be inlined again.
-
-
-
Constructor Detail
-
RecoveryContext
public RecoveryContext(IRMethod irMethod, MethodEntry sourceMethod, DefUseChains defUseChains)
Creates an empty context, deriving the local model from the IR method.- Parameters:
irMethod- SSA method being recoveredsourceMethod- the class file method it came fromdefUseChains- def-use chains over the same IR
-
-
Method Detail
-
getIrMethod
public IRMethod getIrMethod()
- Returns:
- the ir method
-
getSourceMethod
public MethodEntry getSourceMethod()
- Returns:
- the source method
-
getDefUseChains
public DefUseChains getDefUseChains()
- Returns:
- the def use chains
-
getLocals
public MethodLocals getLocals()
- Returns:
- the locals
-
getSlotPartition
public SlotVariablePartition getSlotPartition()
- Returns:
- the reaching-definition partition of local slots into source variables
-
setSlotPartition
public void setSlotPartition(SlotVariablePartition slotPartition)
- Parameters:
slotPartition- partition of local slots into source variables
-
setDebugNameResolver
public void setDebugNameResolver(IntFunction<String> resolver)
Installs the lookup backing debugNameForSlot.- Parameters:
resolver- slot to recovered debug name, or null to disable the lookup
-
debugNameForSlot
public String debugNameForSlot(int slot)
The recovered debug name a slot carries.- Parameters:
slot- local slot index- Returns:
- the recovered name, or null when the class carries none for the slot or the active name-recovery strategy declines it
-
setDebugDescriptorResolver
public void setDebugDescriptorResolver(BiFunction<Integer,Integer,String> resolver)
Installs the lookup backing debugDescriptorAt.- Parameters:
resolver- slot and bytecode offset to LVT descriptor, or null to disable the lookup
-
debugDescriptorAt
public String debugDescriptorAt(int slot, int offset)
The LocalVariableTable type descriptor a slot carries at a bytecode offset.- Parameters:
slot- local slot indexoffset- bytecode offset to read the table at- Returns:
- the descriptor, or null when no resolver is installed or the table has no entry
-
setDebugStoreDescriptorResolver
public void setDebugStoreDescriptorResolver(BiFunction<Integer,Integer,String> resolver)
Installs the lookup backing debugDescriptorAtStore.- Parameters:
resolver- slot and store offset to LVT descriptor, or null to disable the lookup
-
debugDescriptorAtStore
public String debugDescriptorAtStore(int slot, int storeOffset)
The LVT descriptor at the pc where a store takes effect.- Parameters:
slot- local slot indexstoreOffset- bytecode offset of the store- Returns:
- the descriptor, or null when no resolver is installed or the table has no entry
-
getRecoveredExpressions
public Map<SSAValue,Expression> getRecoveredExpressions()
- Returns:
- the recovered expression of each recovered SSA value
-
getInlinedValues
public Set<SSAValue> getInlinedValues()
- Returns:
- values folded into their single use instead of being assigned
-
getVariableNames
public Map<SSAValue,String> getVariableNames()
- Returns:
- the name bound to each named SSA value
-
getSyntheticCounter
public int getSyntheticCounter()
- Returns:
- the counter backing synthetic name generation
-
getPendingNewInstructions
public Map<SSAValue,String> getPendingNewInstructions()
- Returns:
- the class name of each allocation still waiting for its
<init>call
-
getPendingNewLocalSlots
public Map<Integer,String> getPendingNewLocalSlots()
- Returns:
- the class name of the pending new value held in each local slot
-
getDeclaredVariables
public Set<String> getDeclaredVariables()
- Returns:
- the variable names declared in the current scope
-
getDeclaredVariableTypes
public Map<String,SourceType> getDeclaredVariableTypes()
- Returns:
- the declared type of each declared variable name
-
getLocalSlotNames
public Map<Integer,String> getLocalSlotNames()
- Returns:
- the current variable name of each local slot index
-
getMaterializedValues
public Set<SSAValue> getMaterializedValues()
- Returns:
- values assigned to variables, which uses must reference by name instead of inlining
-
getForLoopScopedVariables
public Deque<Set<String>> getForLoopScopedVariables()
- Returns:
- the stack of variable sets declared in for-loop inits, scoped to the loop body
-
getBranchScopedVariables
public Deque<Set<String>> getBranchScopedVariables()
- Returns:
- the stack of variable sets declared in if-then-else branches, scoped to the branch
-
getRecordDeconstructionTemps
public Set<SSAValue> getRecordDeconstructionTemps()
- Returns:
- cast results that are a record deconstruction's synthetic temp (the
(T) selector)
-
getSsaValueSlot
public Map<SSAValue,Integer> getSsaValueSlot()
- Returns:
- the slot index each SSA value was first stored to
-
cacheExpression
public void cacheExpression(SSAValue value, Expression expr)
Stores the recovered expression for a value, replacing any earlier one.- Parameters:
value- value the expression computesexpr- recovered expression
-
getCachedExpression
public Expression getCachedExpression(SSAValue value)
- Parameters:
value- value to look up- Returns:
- the cached expression, or null if the value has not been recovered
-
isRecovered
public boolean isRecovered(SSAValue value)
- Parameters:
value- value to test- Returns:
- true if an expression has been cached for the value
-
markInlined
public void markInlined(SSAValue value)
Records that a value's expression was folded into its single use rather than assigned.- Parameters:
value- value that was inlined
-
isInlined
public boolean isInlined(SSAValue value)
- Parameters:
value- value to test- Returns:
- true if the value was marked inlined
-
setVariableName
public void setVariableName(SSAValue value, String name)
Binds a name to a value, refusing to rename a parameter that already has one.- Parameters:
value- value to namename- name to bind
-
getVariableName
public String getVariableName(SSAValue value)
- Parameters:
value- value to look up- Returns:
- the name bound to the value, or null if it has none
-
nextSyntheticId
public int nextSyntheticId()
- Returns:
- the next synthetic name id, advancing the counter
-
registerPendingNew
public void registerPendingNew(SSAValue result, String className)
Registers an allocation result that awaits its<init>call.- Parameters:
result- value produced by the allocationclassName- class being allocated
-
isPendingNew
public boolean isPendingNew(SSAValue value)
- Parameters:
value- value to test- Returns:
- true if the value is an allocation still waiting for its constructor call
-
consumePendingNew
public String consumePendingNew(SSAValue value)
Removes the pending new record for a value and returns its class name.- Parameters:
value- allocation result- Returns:
- the class name held pending for the value, or null if there is none
-
registerPendingNewLocalSlot
public void registerPendingNewLocalSlot(int localIndex, String className)Registers a local slot as containing a pending new value.- Parameters:
localIndex- local slot the value was stored toclassName- class being allocated
-
consumePendingNewLocalSlot
public String consumePendingNewLocalSlot(int localIndex)
Removes the pending new record for a local slot and returns its class name.- Parameters:
localIndex- local slot index- Returns:
- the class name held pending for the slot, or null if there is none
-
markDeclared
public void markDeclared(String name)
Marks a variable name as declared.- Parameters:
name- variable name being declared
-
markDeclaredWithType
public void markDeclaredWithType(String name, SourceType type)
Marks a variable name as declared, recording its type when one is given.- Parameters:
name- variable name being declaredtype- declared type, or null to record none
-
getDeclaredType
public SourceType getDeclaredType(String name)
- Parameters:
name- variable name to look up- Returns:
- the declared type recorded for the name, or null if none is tracked
-
isDeclared
public boolean isDeclared(String name)
- Parameters:
name- variable name to test- Returns:
- true if the name has been declared
-
clearDeclaredVariables
public void clearDeclaredVariables()
Clears declared variables (for new scope).
-
baselineDeclaredVariables
public void baselineDeclaredVariables()
Snapshots the current declared set as the method's baseline (the pre-declared parameters), so a full re-pass can restore it.
-
resetDeclaredVariablesToBaseline
public void resetDeclaredVariablesToBaseline()
Restores the declared set to the method baseline captured bybaselineDeclaredVariables().
-
markMaterialized
public void markMaterialized(SSAValue value)
Marks an SSA value as materialized into a variable.- Parameters:
value- value that was assigned to a variable
-
isMaterialized
public boolean isMaterialized(SSAValue value)
- Parameters:
value- value to test- Returns:
- true if the value has been materialized into a variable
-
unmarkMaterialized
public void unmarkMaterialized(SSAValue value)
Unmarks an SSA value as materialized, allowing it to be inlined again.- Parameters:
value- value to unmark
-
pinToVariable
public void pinToVariable(SSAValue value)
Pins a value to its variable.- Parameters:
value- value to pin
-
isPinnedToVariable
public boolean isPinnedToVariable(SSAValue value)
- Parameters:
value- value to test- Returns:
- true if the value was pinned and so must never be force-inlined at a use
-
pushForLoopScope
public void pushForLoopScope()
Pushes a new scope for for-loop variables.
-
popForLoopScope
public void popForLoopScope()
Pops the current for-loop scope and removes its variables from declaredVariables.
-
markDeclaredInForLoopInit
public void markDeclaredInForLoopInit(String name)
Marks a variable as declared in a for-loop init expression.- Parameters:
name- variable name being declared
-
pushBranchScope
public void pushBranchScope()
Pushes a new scope for if-then-else branch variables.
-
popBranchScope
public void popBranchScope()
Pops the current branch scope and removes its variables from declaredVariables.
-
markDeclaredInBranch
public void markDeclaredInBranch(String name)
Marks a variable as declared in the current scope.- Parameters:
name- variable name being declared
-
isInBranchScope
public boolean isInBranchScope()
- Returns:
- true while a branch scope is open
-
setLocalSlotName
public void setLocalSlotName(int slotIndex, String name)Binds a variable name to a local slot so later loads from the slot recover the same name.- Parameters:
slotIndex- local slot indexname- name to bind
-
getLocalSlotName
public String getLocalSlotName(int slotIndex)
- Parameters:
slotIndex- local slot index- Returns:
- the name bound to the slot, or null when none has been set
-
setSSAValueSlot
public void setSSAValueSlot(SSAValue value, int slotIndex)
Records the slot an SSA value was stored to, keeping the first one recorded.- Parameters:
value- value that was storedslotIndex- local slot it was stored to
-
getSSAValueSlot
public int getSSAValueSlot(SSAValue value)
- Parameters:
value- value to look up- Returns:
- the slot the value was first stored to, or -1 when it is not tracked
-
parameterSlot
public int parameterSlot(SSAValue value)
- Parameters:
value- value to look up- Returns:
- the local slot a parameter or receiver value occupies, or -1 when the value is neither
-
isParameterOrThisSlot
public boolean isParameterOrThisSlot(int slot)
- Parameters:
slot- local slot index- Returns:
- true when the slot holds the receiver or a parameter rather than a body local
-
parameterIndexForSlot
public int parameterIndexForSlot(int slot)
The zero-based parameter index of a parameter slot, with the receiver excluded from the numbering.- Parameters:
slot- local slot index- Returns:
- the parameter index, or -1 when the slot does not hold a parameter
-
-