001 /*
002 * This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 * This file is licensed to You under the Common Public License (CPL);
005 * You may not use this file except in compliance with the License. You
006 * may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/cpl1.0.php
009 *
010 * See the COPYRIGHT.txt file distributed with this work for information
011 * regarding copyright ownership.
012 */
013 package org.jikesrvm.compilers.opt.ir.operand;
014
015 import org.jikesrvm.VM;
016 import org.jikesrvm.SizeConstants;
017 import org.jikesrvm.classloader.TypeReference;
018 import org.vmmagic.unboxed.Address;
019 import org.vmmagic.unboxed.Extent;
020 import org.vmmagic.unboxed.Offset;
021 import org.vmmagic.unboxed.Word;
022
023 /**
024 * Represents an address constant operand.
025 *
026 * @see Operand
027 */
028 public final class AddressConstantOperand extends ConstantOperand {
029
030 /**
031 * Value of this operand.
032 */
033 public final Address value;
034
035 /**
036 * Constructs a new address constant operand with the specified value.
037 *
038 * @param v value
039 */
040 public AddressConstantOperand(Address v) {
041 value = v;
042 }
043
044 /**
045 * Constructs a new address constant operand with the specified offset value.
046 *
047 * @param v value
048 * TODO: make a separte OffsetConstantOperand
049 */
050 public AddressConstantOperand(Offset v) {
051 this(v.toWord().toAddress());
052 }
053
054 /**
055 * Constructs a new address constant operand with the specified offset value.
056 *
057 * @param v value
058 * TODO: make a separte OffsetConstantOperand
059 */
060 public AddressConstantOperand(Extent v) {
061 this(v.toWord().toAddress());
062 }
063
064 /**
065 * Constructs a new address constant operand with the specified offset value.
066 *
067 * @param v value
068 * TODO: make a separte OffsetConstantOperand
069 */
070 public AddressConstantOperand(Word v) {
071 this(v.toAddress());
072 }
073
074 /**
075 * Return a new operand that is semantically equivalent to <code>this</code>.
076 *
077 * @return a copy of <code>this</code>
078 */
079 public Operand copy() {
080 return new AddressConstantOperand(value);
081 }
082
083 /**
084 * Return the {@link TypeReference} of the value represented by the operand.
085 *
086 * @return TypeReference.Address
087 */
088 public TypeReference getType() {
089 return TypeReference.Address;
090 }
091
092 /**
093 * Does the operand represent a value of the address data type?
094 *
095 * @return <code>true</code>
096 */
097 public boolean isAddress() {
098 return true;
099 }
100
101 /**
102 * Are two operands semantically equivalent?
103 *
104 * @param op other operand
105 * @return <code>true</code> if <code>this</code> and <code>op</code>
106 * are semantically equivalent or <code>false</code>
107 * if they are not.
108 */
109 public boolean similar(Operand op) {
110 return equals(op);
111 }
112
113 public boolean equals(Object o) {
114 return (o instanceof AddressConstantOperand) && (value.EQ(((AddressConstantOperand) o).value));
115 }
116
117 public int hashCode() {
118 return value.toWord().rshl(SizeConstants.LOG_BYTES_IN_ADDRESS).toInt();
119 }
120
121 /**
122 * Returns the string representation of this operand.
123 *
124 * @return a string representation of this operand.
125 */
126 public String toString() {
127 return "Addr " + VM.addressAsHexString(value);
128 }
129 }