1 /*
   2  * Copyright (c) 2009, 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.asm;
  24 
  25 import java.util.ArrayList;
  26 
  27 /**
  28  * This class represents a label within assembly code.
  29  */
  30 public final class Label {
  31 
  32     private int position = -1;
  33     private int blockId = -1;
  34 
  35     /**
  36      * References to instructions that jump to this unresolved label. These instructions need to be
  37      * patched when the label is bound using the {@link #patchInstructions(Assembler)} method.
  38      */
  39     private ArrayList<Integer> patchPositions = null;
  40 
  41     /**
  42      * Returns the position of this label in the code buffer.
  43      *
  44      * @return the position
  45      */
  46     public int position() {
  47         assert position >= 0 : "Unbound label is being referenced";
  48         return position;
  49     }
  50 
  51     public Label() {
  52     }
  53 
  54     public Label(int id) {
  55         blockId = id;
  56     }
  57 
  58     public int getBlockId() {
  59         return blockId;
  60     }
  61 
  62     /**
  63      * Binds the label to the specified position.
  64      *
  65      * @param pos the position
  66      */
  67     protected void bind(int pos) {
  68         this.position = pos;
  69         assert isBound();
  70     }
  71 
  72     public boolean isBound() {
  73         return position >= 0;
  74     }
  75 
  76     public void addPatchAt(int branchLocation) {
  77         assert !isBound() : "Label is already bound " + this + " " + branchLocation + " at position " + position;
  78         if (patchPositions == null) {
  79             patchPositions = new ArrayList<>(2);
  80         }
  81         patchPositions.add(branchLocation);
  82     }
  83 
  84     protected void patchInstructions(Assembler masm) {
  85         assert isBound() : "Label should be bound";
  86         if (patchPositions != null) {
  87             int target = position;
  88             for (int i = 0; i < patchPositions.size(); ++i) {
  89                 int pos = patchPositions.get(i);
  90                 masm.patchJumpTarget(pos, target);
  91             }
  92         }
  93     }
  94 
  95     public void reset() {
  96         if (this.patchPositions != null) {
  97             this.patchPositions.clear();
  98         }
  99         this.position = -1;
 100     }
 101 
 102     @Override
 103     public String toString() {
 104         return isBound() ? String.valueOf(position()) : "?";
 105     }
 106 }