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

org.parboiled.transform.InstructionGraphNode Maven / Gradle / Ivy

There is a newer version: 1.2.0
Show newest version
/*
 * Copyright (c) 2009-2010 Ken Wenzel and Mathias Doenitz
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.parboiled.transform;

import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.analysis.BasicValue;
import org.objectweb.asm.util.Printer;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static org.parboiled.common.Preconditions.checkArgNotNull;
import static org.objectweb.asm.Opcodes.IALOAD;
import static org.objectweb.asm.Opcodes.IASTORE;
import static org.objectweb.asm.Opcodes.ILOAD;
import static org.objectweb.asm.Opcodes.ISTORE;

/**
 * A node in the instruction dependency graph.
 */
class InstructionGraphNode extends BasicValue {

    private AbstractInsnNode instruction;
    private final BasicValue resultValue;
    private final List predecessors = new ArrayList();
    private boolean isActionRoot;
    private final boolean isVarInitRoot;
    private final boolean isCallOnContextAware;
    private final boolean isXLoad;
    private final boolean isXStore;
    private InstructionGroup group;

    public InstructionGraphNode(AbstractInsnNode instruction, BasicValue resultValue) {
        super(null);
        this.instruction = instruction;
        this.resultValue = resultValue;
        this.isActionRoot = AsmUtils.isActionRoot(instruction);
        this.isVarInitRoot = AsmUtils.isVarRoot(instruction);
        this.isCallOnContextAware = AsmUtils.isCallOnContextAware(instruction);
        this.isXLoad = ILOAD <= instruction.getOpcode() && instruction.getOpcode() < IALOAD;
        this.isXStore = ISTORE <= instruction.getOpcode() && instruction.getOpcode() < IASTORE;
    }

    public int getSize() {
        return resultValue.getSize();
    }

    public AbstractInsnNode getInstruction() {
        return instruction;
    }

    public void setInstruction(AbstractInsnNode instruction) {
        this.instruction = instruction;
    }

    public BasicValue getResultValue() {
        return resultValue;
    }

    public List getPredecessors() {
        return predecessors;
    }

    public InstructionGroup getGroup() {
        return group;
    }

    public void setGroup(InstructionGroup newGroup) {
        if (newGroup != group) {
            if (group != null) {
                group.getNodes().remove(this);
            }
            group = newGroup;
            if (group != null) {
                group.getNodes().add(this);
            }
        }
    }

    public boolean isActionRoot() {
        return isActionRoot;
    }

    public void setIsActionRoot() {
        isActionRoot = true;
    }

    public boolean isVarInitRoot() {
        return isVarInitRoot;
    }

    public boolean isCallOnContextAware() {
        return isCallOnContextAware;
    }

    public boolean isXLoad() {
        return isXLoad;
    }

    public boolean isXStore() {
        return isXStore;
    }

    public void addPredecessors(Collection preds) {
        checkArgNotNull(preds, "preds");
        for (BasicValue pred : preds) {
            if (pred instanceof InstructionGraphNode) {
                addPredecessor(((InstructionGraphNode) pred));
            }
        }
    }

    public void addPredecessor(InstructionGraphNode node) {
        if (!predecessors.contains(node)) {
            predecessors.add(node);
        }
    }

    @Override
    public boolean equals(Object value) {
        return value == this;
    }

    @Override
    public int hashCode() {
        return System.identityHashCode(this);
    }

    @Override
    public String toString() {
        return instruction.getOpcode() != -1 ? Printer.OPCODES[instruction.getOpcode()] : super.toString();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy