001/*
002 * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir;
024
025import com.oracle.graal.asm.*;
026import com.oracle.graal.compiler.common.cfg.*;
027import com.oracle.graal.lir.StandardOp.BranchOp;
028import com.oracle.graal.lir.StandardOp.JumpOp;
029
030/**
031 * LIR instructions such as {@link JumpOp} and {@link BranchOp} need to reference their target
032 * {@link AbstractBlockBase}. However, direct references are not possible since the control flow
033 * graph (and therefore successors lists) can be changed by optimizations - and fixing the
034 * instructions is error prone. Therefore, we represent an edge to block B from block A via the
035 * tuple {@code (A,
036 * successor-index-of-B)}. That is, indirectly by storing the index into the successor list of A.
037 * Note therefore that the successor list cannot be re-ordered.
038 */
039public final class LabelRef {
040
041    private final LIR lir;
042    private final AbstractBlockBase<?> block;
043    private final int suxIndex;
044
045    /**
046     * Returns a new reference to a successor of the given block.
047     *
048     * @param block The base block that contains the successor list.
049     * @param suxIndex The index of the successor.
050     * @return The newly created label reference.
051     */
052    public static LabelRef forSuccessor(final LIR lir, final AbstractBlockBase<?> block, final int suxIndex) {
053        return new LabelRef(lir, block, suxIndex);
054    }
055
056    /**
057     * Returns a new reference to a successor of the given block.
058     *
059     * @param block The base block that contains the successor list.
060     * @param suxIndex The index of the successor.
061     */
062    private LabelRef(final LIR lir, final AbstractBlockBase<?> block, final int suxIndex) {
063        this.lir = lir;
064        this.block = block;
065        this.suxIndex = suxIndex;
066    }
067
068    public AbstractBlockBase<?> getSourceBlock() {
069        return block;
070    }
071
072    public AbstractBlockBase<?> getTargetBlock() {
073        return block.getSuccessors().get(suxIndex);
074    }
075
076    public Label label() {
077        return ((StandardOp.LabelOp) lir.getLIRforBlock(getTargetBlock()).get(0)).getLabel();
078    }
079
080    @Override
081    public String toString() {
082        return getSourceBlock() + " -> " + (suxIndex < block.getSuccessors().size() ? getTargetBlock() : "?");
083    }
084}