view graal/com.oracle.max.graal.compiler/src/com/oracle/max/graal/compiler/ir/ExceptionDispatch.java @ 2874:d90bf514d647

Renamed packages.
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 08 Jun 2011 08:59:54 +0200
parents graal/com.oracle.max.graal.compiler/src/com/sun/c1x/ir/ExceptionDispatch.java@0341b6424579
children 60a58915c94d
line wrap: on
line source

/*
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.max.graal.compiler.ir;

import com.oracle.max.graal.compiler.debug.*;
import com.oracle.max.graal.graph.*;
import com.sun.cri.ci.*;
import com.sun.cri.ri.*;

/**
 * This instruction takes an exception object and has two successors:
 * The catchSuccessor is called whenever the exception matches the given type, otherwise otherSuccessor is called.
 */
public final class ExceptionDispatch extends BlockEnd {

    private static final int INPUT_COUNT = 1;
    private static final int INPUT_EXCEPTION = 0;

    private static final int SUCCESSOR_COUNT = 0;

    @Override
    protected int inputCount() {
        return super.inputCount() + INPUT_COUNT;
    }

    @Override
    protected int successorCount() {
        return super.successorCount() + SUCCESSOR_COUNT;
    }

    /**
     * The instruction producing the exception object.
     */
     public Value exception() {
        return (Value) inputs().get(super.inputCount() + INPUT_EXCEPTION);
    }

    public Value setException(Value n) {
        return (Value) inputs().set(super.inputCount() + INPUT_EXCEPTION, n);
    }

    private final RiType catchType;

    /**
     * Constructs a new ExceptionDispatch instruction.
     */
    public ExceptionDispatch(Value exception, Instruction catchSuccessor, Instruction otherSuccessor, RiType catchType, Graph graph) {
        super(CiKind.Int, 2, INPUT_COUNT, SUCCESSOR_COUNT, graph);
        setException(exception);
        setBlockSuccessor(0, otherSuccessor);
        setBlockSuccessor(1, catchSuccessor);
        this.catchType = catchType;
    }

    public RiType catchType() {
        return catchType;
    }

    /**
     * Gets the block corresponding to the catch block.
     * @return the true successor
     */
    public Instruction catchSuccessor() {
        return blockSuccessor(1);
    }

    /**
     * Gets the block corresponding to the rest of the dispatch chain.
     * @return the false successor
     */
    public Instruction otherSuccessor() {
        return blockSuccessor(0);
    }

    /**
     * Gets the block corresponding to the specified outcome of the branch.
     * @param istrue {@code true} if the true successor is requested, {@code false} otherwise
     * @return the corresponding successor
     */
    public Instruction successor(boolean istrue) {
        return blockSuccessor(istrue ? 1 : 0);
    }

    @Override
    public void accept(ValueVisitor v) {
        v.visitExceptionDispatch(this);
    }

    @Override
    public void print(LogStream out) {
        out.print("exception_dispatch ").
        print(exception()).
        print(' ').
        print("instanceof").
        print(' ').
        print(catchType().name()).
        print(" then ").
        print(blockSuccessors().get(1).toString()).
        print(" else B").
        print(blockSuccessors().get(0).toString());
    }

    @Override
    public String shortName() {
        return "Dispatch " + catchType().name();
    }

    @Override
    public Node copy(Graph into) {
        ExceptionDispatch x = new ExceptionDispatch(null, null, null, catchType, into);
        return x;
    }
}