001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.nodes.calc;
024
025import java.nio.*;
026
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.type.*;
030import com.oracle.graal.graph.*;
031import com.oracle.graal.graph.spi.*;
032import com.oracle.graal.lir.gen.*;
033import com.oracle.graal.nodeinfo.*;
034import com.oracle.graal.nodes.*;
035import com.oracle.graal.nodes.spi.*;
036
037/**
038 * The {@code ReinterpretNode} class represents a reinterpreting conversion that changes the stamp
039 * of a primitive value to some other incompatible stamp. The new stamp must have the same width as
040 * the old stamp.
041 */
042@NodeInfo
043public final class ReinterpretNode extends UnaryNode implements ArithmeticLIRLowerable {
044
045    public static final NodeClass<ReinterpretNode> TYPE = NodeClass.create(ReinterpretNode.class);
046
047    public ReinterpretNode(Kind to, ValueNode value) {
048        this(StampFactory.forKind(to), value);
049    }
050
051    public ReinterpretNode(Stamp to, ValueNode value) {
052        super(TYPE, to, value);
053        assert to instanceof ArithmeticStamp;
054    }
055
056    private SerializableConstant evalConst(SerializableConstant c) {
057        /*
058         * We don't care about byte order here. Either would produce the correct result.
059         */
060        ByteBuffer buffer = ByteBuffer.wrap(new byte[c.getSerializedSize()]).order(ByteOrder.nativeOrder());
061        c.serialize(buffer);
062
063        buffer.rewind();
064        SerializableConstant ret = ((ArithmeticStamp) stamp()).deserialize(buffer);
065
066        assert !buffer.hasRemaining();
067        return ret;
068    }
069
070    @Override
071    public ValueNode canonical(CanonicalizerTool tool, ValueNode forValue) {
072        if (forValue.isConstant()) {
073            return ConstantNode.forConstant(stamp(), evalConst((SerializableConstant) forValue.asConstant()), null);
074        }
075        if (stamp().isCompatible(forValue.stamp())) {
076            return forValue;
077        }
078        if (forValue instanceof ReinterpretNode) {
079            ReinterpretNode reinterpret = (ReinterpretNode) forValue;
080            return new ReinterpretNode(stamp(), reinterpret.getValue());
081        }
082        return this;
083    }
084
085    @Override
086    public void generate(NodeValueMap nodeValueMap, ArithmeticLIRGenerator gen) {
087        LIRKind kind = gen.getLIRKind(stamp());
088        nodeValueMap.setResult(this, gen.emitReinterpret(kind, nodeValueMap.operand(getValue())));
089    }
090
091    public static ValueNode reinterpret(Kind toKind, ValueNode value) {
092        return value.graph().unique(new ReinterpretNode(toKind, value));
093    }
094
095    @NodeIntrinsic
096    public static native float reinterpret(@ConstantNodeParameter Kind kind, int value);
097
098    @NodeIntrinsic
099    public static native int reinterpret(@ConstantNodeParameter Kind kind, float value);
100
101    @NodeIntrinsic
102    public static native double reinterpret(@ConstantNodeParameter Kind kind, long value);
103
104    @NodeIntrinsic
105    public static native long reinterpret(@ConstantNodeParameter Kind kind, double value);
106}