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