001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.editor;
022:
023: /**
024: * <tt>Label</tt> is used to label an instruction. <tt>Label</tt>s are used
025: * to preserve the location of branch targets. A <tt>Label</tt> consists of an
026: * index into the code array and a <tt>boolean</tt> that determines whether or
027: * not it starts a basic block.
028: *
029: * @author Nate Nystrom (<a
030: * href="mailto:nystrom@cs.purdue.edu">nystrom@cs.purdue.edu</a>)
031: */
032: public class Label {
033: public static boolean TRACE = false;
034:
035: private int index;
036:
037: private boolean startsBlock;
038:
039: private String comment; // Comment with Label
040:
041: /**
042: * Constructor.
043: *
044: * @param index
045: * A unique index for the label. For instance, its offset in the
046: * instruction array.
047: */
048: public Label(final int index) {
049: this (index, false);
050: }
051:
052: /**
053: * Constructor.
054: *
055: * @param index
056: * The index of this label into the instruction array
057: * @param startsBlock
058: * True if the label is the first instruction in a basic block,
059: * false if not.
060: */
061: public Label(final int index, final boolean startsBlock) {
062: this .index = index;
063: this .startsBlock = startsBlock;
064:
065: // if(Label.TRACE) {
066: // try {
067: // throw new Exception("Creating a new label: " + this);
068: // } catch(Exception ex) {
069: // ex.printStackTrace(System.out);
070: // }
071: // }
072: }
073:
074: /**
075: * Sets the comment associated with this <tt>Label</tt>.
076: */
077: public void setComment(final String comment) {
078: this .comment = comment;
079: }
080:
081: /**
082: * Set if the label starts a block.
083: *
084: * @param startsBlock
085: * True if the label starts a block, false if not.
086: */
087: public void setStartsBlock(final boolean startsBlock) {
088: this .startsBlock = startsBlock;
089: }
090:
091: /**
092: * Check if the label starts a block.
093: *
094: * @return True if the label starts a block, false if not.
095: */
096: public boolean startsBlock() {
097: return startsBlock;
098: }
099:
100: /**
101: * Get the index of the label.
102: *
103: * @return The index of the label.
104: */
105: public int index() {
106: return index;
107: }
108:
109: /**
110: * Hash the label.
111: *
112: * @return The hash code.
113: */
114: public int hashCode() {
115: return index;
116: }
117:
118: /**
119: * Check if an object is equal to this label.
120: *
121: * @param obj
122: * The object to compare against.
123: * @return true if equal, false if not.
124: */
125: public boolean equals(final Object obj) {
126: return ((obj instanceof Label) && (((Label) obj).index == index));
127: }
128:
129: /**
130: * Convert the label to a string.
131: *
132: * @return A string representation of the label.
133: */
134: public String toString() {
135: if (comment != null) {
136: return "label_" + index + " (" + comment + ")";
137: } else {
138: return "label_" + index;
139: }
140: }
141: }
|