Class LoweringContext


  • public class LoweringContext
    extends Object
    Shared mutable state for AST-to-IR lowering: variable bindings, the current block, and break/continue targets.
    • Constructor Detail

      • LoweringContext

        public LoweringContext​(IRMethod irMethod,
                               ConstPool constPool,
                               TypeResolver typeResolver)
        Creates a lowering context for one method.
        Parameters:
        irMethod - the IR method being built
        constPool - the constant pool receiving new entries
        typeResolver - 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
      • 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 name
        localIndex - slot the parameter arrives in
        value - 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 local
        type - declared type
        isParameter - true when the local is a parameter or the receiver
      • declareLocal

        public void declareLocal​(String name,
                                 IRType type,
                                 boolean isParameter,
                                 String signature)
        As declareLocal(String, IRType, boolean) with the declared type's generic signature.
        Parameters:
        name - source name of the local
        type - declared type
        isParameter - true when the local is a parameter or the receiver
        signature - 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 assigned
        value - 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 loops
        continueTarget - block to jump to for continue
        breakTarget - block to jump to for break
        finallyDepth - 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 a break jumps 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 a break targets - 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 a continue targets - 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 label
        target - 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
      • clearArrayConstructors

        public void clearArrayConstructors()
        Clears array constructors after they have been processed.