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 org.jikesrvm.classloader.TypeReference;
016import org.jikesrvm.runtime.Entrypoints;
017import org.vmmagic.unboxed.Offset;
018
019/**
020 * Represents a constant double operand.
021 *
022 * @see Operand
023 */
024public final class DoubleConstantOperand extends ConstantOperand {
025
026  /**
027   * Value of this operand.
028   */
029  public double value;
030
031  /**
032   * Offset in JTOC where this double constant lives. (0 for constants
033   * obtained from constant folding)
034   */
035  public Offset offset;
036
037  /**
038   * Constructs a new double constant operand with the specified value.
039   *
040   * @param v value
041   */
042  public DoubleConstantOperand(double v) {
043    value = v;
044    if (v == 0.) {
045      offset = Entrypoints.zeroDoubleField.getOffset();
046    } else if (v == 1.) {
047      offset = Entrypoints.oneDoubleField.getOffset();
048    } else {
049      offset = Offset.zero();
050    }
051  }
052
053  /**
054   * Constructs a new double constant operand with the specified value and JTOC offset.
055   *
056   * @param v value
057   * @param i offset in the jtoc
058   */
059  public DoubleConstantOperand(double v, Offset i) {
060    value = v;
061    offset = i;
062  }
063
064  @Override
065  public Operand copy() {
066    return new DoubleConstantOperand(value, offset);
067  }
068
069  /**
070   * @return {@link TypeReference#Double}
071   */
072  @Override
073  public TypeReference getType() {
074    return TypeReference.Double;
075  }
076
077  /**
078   * @return <code>true</code>
079   */
080  @Override
081  public boolean isDouble() {
082    return true;
083  }
084
085  @Override
086  public boolean similar(Operand op) {
087    return (op instanceof DoubleConstantOperand) && (value == ((DoubleConstantOperand) op).value);
088  }
089
090  /**
091   * Returns the string representation of this operand.
092   *
093   * @return a string representation of this operand.
094   */
095  @Override
096  public String toString() {
097    return Double.toString(value) + "D";
098  }
099
100}