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