All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.qbicc.graph.Ret Maven / Gradle / Ivy

There is a newer version: 0.77.0
Show newest version
package org.qbicc.graph;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.qbicc.graph.literal.BlockLiteral;
import org.qbicc.type.definition.element.ExecutableElement;

/**
 * An indirect branch node, used for implementing the legacy {@code ret} bytecode and also useful for outlining.
 */
public final class Ret extends AbstractTerminator implements Terminator {
    private final Node dependency;
    private final Value returnAddressValue;
    private final BasicBlock terminatedBlock;
    private List successors;

    Ret(final Node callSite, final ExecutableElement element, final int line, final int bci, final BlockEntry blockEntry, final Node dependency, final Value returnAddressValue, Map targetArguments) {
        super(callSite, element, line, bci, targetArguments);
        terminatedBlock = new BasicBlock(blockEntry, this);
        this.dependency = dependency;
        this.returnAddressValue = returnAddressValue;
    }

    public BasicBlock getTerminatedBlock() {
        return terminatedBlock;
    }

    public Value getReturnAddressValue() {
        return returnAddressValue;
    }

    public int getValueDependencyCount() {
        return 1;
    }

    public Value getValueDependency(final int index) throws IndexOutOfBoundsException {
        return index == 0 ? returnAddressValue : Util.throwIndexOutOfBounds(index);
    }

    @Override
    public Node getDependency() {
        return dependency;
    }

    public  R accept(final TerminatorVisitor visitor, final T param) {
        return visitor.visit(param, this);
    }

    int calcHashCode() {
        return Objects.hash(dependency, returnAddressValue);
    }

    @Override
    public int getSuccessorCount() {
        return getSuccessors().size();
    }

    @Override
    public BasicBlock getSuccessor(int index) {
        return getSuccessors().get(index);
    }

    public List getSuccessors() {
        List successors = this.successors;
        if (successors == null) {
            // calculate once
            if (returnAddressValue instanceof BlockLiteral bl) {
                return this.successors = List.of(bl.getBlock());
            } else {
                if (returnAddressValue instanceof BlockParameter bp) {
                    Set possibleValues = bp.getPossibleValues();
                    successors = new ArrayList<>(possibleValues.size());
                    for (Value possibleValue : possibleValues) {
                        if (possibleValue instanceof BlockLiteral bl) {
                            successors.add(bl.getBlock());
                        } else {
                            throw invalidSuccessor();
                        }
                    }
                    return this.successors = List.copyOf(successors);
                } else {
                    throw invalidSuccessor();
                }
            }
        } else {
            return successors;
        }
    }

    private static IllegalArgumentException invalidSuccessor() {
        return new IllegalArgumentException("Invalid successor for `Ret` node");
    }

    @Override
    String getNodeName() {
        return "Ret";
    }

    public boolean equals(final Object other) {
        return other instanceof Ret && equals((Ret) other);
    }

    public boolean equals(final Ret other) {
        return this == other || other != null
            && dependency.equals(other.dependency)
            && returnAddressValue.equals(other.returnAddressValue);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy