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      @Override
068      public Operand copy() {
069        return new FloatConstantOperand(value, offset);
070      }
071    
072      /**
073       * @return {@link TypeReference#Float}
074       */
075      @Override
076      public TypeReference getType() {
077        return TypeReference.Float;
078      }
079    
080      /**
081       * @return <code>true</code>
082       */
083      @Override
084      public boolean isFloat() {
085        return true;
086      }
087    
088      @Override
089      public boolean similar(Operand op) {
090        return (op instanceof FloatConstantOperand) && (value == ((FloatConstantOperand) op).value);
091      }
092    
093      /**
094       * Returns the string representation of this operand.
095       *
096       * @return a string representation of this operand.
097       */
098      @Override
099      public String toString() {
100        return Float.toString(value);
101      }
102    }