1 /*
   2  * Copyright (c) 2009, 2018, 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 import org.graalvm.compiler.debug.GraalError;
  30 
  31 /**
  32  * This class represents a label within assembly code.
  33  */
  34 public final class Label {
  35 
  36     private int position = -1;
  37     private int blockId = -1;
  38 
  39     /**
  40      * Positions of instructions that jump to this unresolved label. These instructions are patched
  41      * when the label is bound.
  42      */
  43     ArrayList<Integer> patchPositions;
  44 
  45     /**
  46      * Link in list of labels with instructions to be patched.
  47      */
  48     Label nextWithPatches;
  49 
  50     /**
  51      * Returns the position of this label in the code buffer.
  52      *
  53      * @return the position
  54      */
  55     public int position() {
  56         assert position >= 0 : "Unbound label is being referenced";
  57         return position;
  58     }
  59 
  60     public Label() {
  61     }
  62 
  63     public Label(int id) {
  64         blockId = id;
  65     }
  66 
  67     public int getBlockId() {
  68         return blockId;
  69     }
  70 
  71     /**
  72      * Binds the label to {@code pos} and patches all instructions added by
  73      * {@link #addPatchAt(int, Assembler)}.
  74      */
  75     protected void bind(int pos, Assembler asm) {
  76         if (pos < 0) {
  77             throw new GraalError("Cannot bind label to negative position %d", pos);
  78         }
  79         this.position = pos;
  80         if (patchPositions != null) {
  81             for (int i = 0; i < patchPositions.size(); ++i) {
  82                 asm.patchJumpTarget(patchPositions.get(i), position);
  83             }
  84             patchPositions = null;
  85         }
  86     }
  87 
  88     public boolean isBound() {
  89         return position >= 0;
  90     }
  91 
  92     public void addPatchAt(int branchLocation, Assembler asm) {
  93         assert !isBound() : "Label is already bound " + this + " " + branchLocation + " at position " + position;
  94         if (patchPositions == null) {
  95             patchPositions = new ArrayList<>(2);
  96             nextWithPatches = asm.labelsWithPatches;
  97             asm.labelsWithPatches = this;
  98         }
  99         patchPositions.add(branchLocation);
 100 
 101     }
 102 
 103     public void reset() {
 104         if (this.patchPositions != null) {
 105             this.patchPositions.clear();
 106         }
 107         this.position = -1;
 108     }
 109 
 110     @Override
 111     public String toString() {
 112         return isBound() ? String.valueOf(position()) : "?";
 113     }
 114 }