1 /*
   2  * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.lir;
  24 
  25 import org.graalvm.compiler.asm.Label;
  26 import org.graalvm.compiler.core.common.cfg.AbstractBlockBase;
  27 import org.graalvm.compiler.lir.StandardOp.BranchOp;
  28 import org.graalvm.compiler.lir.StandardOp.JumpOp;
  29 
  30 /**
  31  * LIR instructions such as {@link JumpOp} and {@link BranchOp} need to reference their target
  32  * {@link AbstractBlockBase}. However, direct references are not possible since the control flow
  33  * graph (and therefore successors lists) can be changed by optimizations - and fixing the
  34  * instructions is error prone. Therefore, we represent an edge to block B from block A via the
  35  * tuple {@code (A,
  36  * successor-index-of-B)}. That is, indirectly by storing the index into the successor list of A.
  37  * Note therefore that the successor list cannot be re-ordered.
  38  */
  39 public final class LabelRef {
  40 
  41     private final LIR lir;
  42     private final AbstractBlockBase<?> block;
  43     private final int suxIndex;
  44 
  45     /**
  46      * Returns a new reference to a successor of the given block.
  47      *
  48      * @param block The base block that contains the successor list.
  49      * @param suxIndex The index of the successor.
  50      * @return The newly created label reference.
  51      */
  52     public static LabelRef forSuccessor(final LIR lir, final AbstractBlockBase<?> block, final int suxIndex) {
  53         return new LabelRef(lir, block, suxIndex);
  54     }
  55 
  56     /**
  57      * Returns a new reference to a successor of the given block.
  58      *
  59      * @param block The base block that contains the successor list.
  60      * @param suxIndex The index of the successor.
  61      */
  62     private LabelRef(final LIR lir, final AbstractBlockBase<?> block, final int suxIndex) {
  63         this.lir = lir;
  64         this.block = block;
  65         this.suxIndex = suxIndex;
  66     }
  67 
  68     public AbstractBlockBase<?> getSourceBlock() {
  69         return block;
  70     }
  71 
  72     public AbstractBlockBase<?> getTargetBlock() {
  73         return block.getSuccessors()[suxIndex];
  74     }
  75 
  76     public Label label() {
  77         return ((StandardOp.LabelOp) lir.getLIRforBlock(getTargetBlock()).get(0)).getLabel();
  78     }
  79 
  80     @Override
  81     public String toString() {
  82         return getSourceBlock() + " -> " + (suxIndex < block.getSuccessors().length ? getTargetBlock() : "?");
  83     }
  84 }