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 com.oracle.graal.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 
  34     /**
  35      * References to instructions that jump to this unresolved label. These instructions need to be
  36      * patched when the label is bound using the {@link #patchInstructions(AbstractAssembler)}
  37      * method.
  38      */
  39     private ArrayList<Integer> patchPositions = new ArrayList<>(4);
  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     /**
  55      * Binds the label to the specified position.
  56      * 
  57      * @param pos the position
  58      */
  59     protected void bind(int pos) {
  60         this.position = pos;
  61         assert isBound();
  62     }
  63 
  64     public boolean isBound() {
  65         return position >= 0;
  66     }
  67 
  68     public void addPatchAt(int branchLocation) {
  69         assert !isBound() : "Label is already bound";
  70         patchPositions.add(branchLocation);
  71     }
  72 
  73     protected void patchInstructions(AbstractAssembler masm) {
  74         assert isBound() : "Label should be bound";
  75         int target = position;
  76         for (int i = 0; i < patchPositions.size(); ++i) {
  77             int pos = patchPositions.get(i);
  78             masm.patchJumpTarget(pos, target);
  79         }
  80     }
  81 
  82     @Override
  83     public String toString() {
  84         return isBound() ? String.valueOf(position()) : "?";
  85     }
  86 }
--- EOF ---