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     */
013    package org.jikesrvm.compilers.opt.ir.operand;
014    
015    import org.jikesrvm.SizeConstants;
016    import org.jikesrvm.classloader.TypeReference;
017    import org.jikesrvm.runtime.Entrypoints;
018    import org.vmmagic.unboxed.Offset;
019    
020    /**
021     * Represents a constant double operand.
022     *
023     * @see Operand
024     */
025    
026    public final class DoubleConstantOperand extends ConstantOperand implements SizeConstants {
027    
028      /**
029       * Value of this operand.
030       */
031      public double value;
032    
033      /**
034       * Offset in JTOC where this double constant lives. (0 for constants
035       * obtained from constant folding)
036       */
037      public Offset offset;
038    
039      /**
040       * Constructs a new double constant operand with the specified value.
041       *
042       * @param v value
043       */
044      public DoubleConstantOperand(double v) {
045        value = v;
046        if (v == 0.) {
047          offset = Entrypoints.zeroDoubleField.getOffset();
048        } else if (v == 1.) {
049          offset = Entrypoints.oneDoubleField.getOffset();
050        } else {
051          offset = Offset.zero();
052        }
053      }
054    
055      /**
056       * Constructs a new double constant operand with the specified value and JTOC offset.
057       *
058       * @param v value
059       * @param i offset in the jtoc
060       */
061      public DoubleConstantOperand(double v, Offset i) {
062        value = v;
063        offset = i;
064      }
065    
066      /**
067       * Return a new operand that is semantically equivalent to <code>this</code>.
068       *
069       * @return a copy of <code>this</code>
070       */
071      public Operand copy() {
072        return new DoubleConstantOperand(value, offset);
073      }
074    
075      /**
076       * Return the {@link TypeReference} of the value represented by the operand.
077       *
078       * @return TypeReference.Double
079       */
080      public TypeReference getType() {
081        return TypeReference.Double;
082      }
083    
084      /**
085       * Does the operand represent a value of the double data type?
086       *
087       * @return <code>true</code>
088       */
089      public boolean isDouble() {
090        return true;
091      }
092    
093      /**
094       * Are two operands semantically equivalent?
095       *
096       * @param op other operand
097       * @return   <code>true</code> if <code>this</code> and <code>op</code>
098       *           are semantically equivalent or <code>false</code>
099       *           if they are not.
100       */
101      public boolean similar(Operand op) {
102        return (op instanceof DoubleConstantOperand) && (value == ((DoubleConstantOperand) op).value);
103      }
104    
105      /**
106       * Returns the string representation of this operand.
107       *
108       * @return a string representation of this operand.
109       */
110      public String toString() {
111        return Double.toString(value) + "D";
112      }
113    
114    }