001/* 002 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.asm; 024 025import java.util.*; 026 027/** 028 * This class represents a label within assembly code. 029 */ 030public final class Label { 031 032 private int position = -1; 033 private int blockId = -1; 034 035 /** 036 * References to instructions that jump to this unresolved label. These instructions need to be 037 * patched when the label is bound using the {@link #patchInstructions(Assembler)} method. 038 */ 039 private ArrayList<Integer> patchPositions = null; 040 041 /** 042 * Returns the position of this label in the code buffer. 043 * 044 * @return the position 045 */ 046 public int position() { 047 assert position >= 0 : "Unbound label is being referenced"; 048 return position; 049 } 050 051 public Label() { 052 } 053 054 public Label(int id) { 055 blockId = id; 056 } 057 058 public int getBlockId() { 059 return blockId; 060 } 061 062 /** 063 * Binds the label to the specified position. 064 * 065 * @param pos the position 066 */ 067 protected void bind(int pos) { 068 this.position = pos; 069 assert isBound(); 070 } 071 072 public boolean isBound() { 073 return position >= 0; 074 } 075 076 public void addPatchAt(int branchLocation) { 077 assert !isBound() : "Label is already bound " + this + " " + branchLocation + " at position " + position; 078 if (patchPositions == null) { 079 patchPositions = new ArrayList<>(2); 080 } 081 patchPositions.add(branchLocation); 082 } 083 084 protected void patchInstructions(Assembler masm) { 085 assert isBound() : "Label should be bound"; 086 if (patchPositions != null) { 087 int target = position; 088 for (int i = 0; i < patchPositions.size(); ++i) { 089 int pos = patchPositions.get(i); 090 masm.patchJumpTarget(pos, target); 091 } 092 } 093 } 094 095 public void reset() { 096 if (this.patchPositions != null) { 097 this.patchPositions.clear(); 098 } 099 this.position = -1; 100 } 101 102 @Override 103 public String toString() { 104 return isBound() ? String.valueOf(position()) : "?"; 105 } 106}