001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
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/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.opt.ir.operand;
014
015import static org.jikesrvm.runtime.UnboxedSizeConstants.LOG_BYTES_IN_ADDRESS;
016
017import org.jikesrvm.classloader.TypeReference;
018import org.jikesrvm.util.Services;
019import org.vmmagic.unboxed.Address;
020import org.vmmagic.unboxed.Extent;
021import org.vmmagic.unboxed.Offset;
022import org.vmmagic.unboxed.Word;
023
024/**
025 * Represents an address constant operand.
026 *
027 * @see Operand
028 */
029public final class AddressConstantOperand extends ConstantOperand {
030
031  /**
032   * Value of this operand.
033   */
034  public final Address value;
035
036  /**
037   * Constructs a new address constant operand with the specified value.
038   *
039   * @param v value
040   */
041  public AddressConstantOperand(Address v) {
042    value = v;
043  }
044
045  /**
046   * Constructs a new address constant operand with the specified offset value.
047   *
048   * @param v value
049   * TODO: make a separte OffsetConstantOperand
050   */
051  public AddressConstantOperand(Offset v) {
052    this(v.toWord().toAddress());
053  }
054
055  /**
056   * Constructs a new address constant operand with the specified offset value.
057   *
058   * @param v value
059   * TODO: make a separate OffsetConstantOperand
060   */
061  public AddressConstantOperand(Extent v) {
062    this(v.toWord().toAddress());
063  }
064
065  /**
066   * Constructs a new address constant operand with the specified offset value.
067   *
068   * @param v value
069   * TODO: make a separate OffsetConstantOperand
070   */
071  public AddressConstantOperand(Word v) {
072    this(v.toAddress());
073  }
074
075  @Override
076  public Operand copy() {
077    return new AddressConstantOperand(value);
078  }
079
080  /**
081   * @return {@link TypeReference#Address}
082   */
083  @Override
084  public TypeReference getType() {
085    return TypeReference.Address;
086  }
087
088  /**
089   * @return <code>true</code>
090   */
091  @Override
092  public boolean isAddress() {
093    return true;
094  }
095
096  @Override
097  public boolean similar(Operand op) {
098    return equals(op);
099  }
100
101  @Override
102  public boolean equals(Object o) {
103    return (o instanceof AddressConstantOperand) && (value.EQ(((AddressConstantOperand) o).value));
104  }
105
106  @Override
107  public int hashCode() {
108    return value.toWord().rshl(LOG_BYTES_IN_ADDRESS).toInt();
109  }
110
111  /**
112   * Returns the string representation of this operand.
113   *
114   * @return a string representation of this operand.
115   */
116  @Override
117  public String toString() {
118    return "Addr " + Services.addressAsHexString(value);
119  }
120}