view graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/frame/FrameGetNode.java @ 11959:23ccaa863eda

made CodeCacheProvider independent of MetaAccessProvider (GRAAL-511)
author Doug Simon <doug.simon@oracle.com>
date Thu, 10 Oct 2013 16:14:55 +0200
parents 88fb9cba8751
children
line wrap: on
line source

/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package com.oracle.graal.truffle.nodes.frame;

import sun.misc.*;

import com.oracle.graal.api.meta.*;
import com.oracle.graal.graph.*;
import com.oracle.graal.nodes.*;
import com.oracle.graal.nodes.calc.*;
import com.oracle.graal.nodes.extended.*;
import com.oracle.graal.nodes.java.*;
import com.oracle.graal.nodes.spi.*;
import com.oracle.graal.nodes.type.*;
import com.oracle.graal.nodes.virtual.*;
import com.oracle.graal.truffle.*;
import com.oracle.truffle.api.frame.*;

/**
 * Intrinsic node for read access to a Truffle frame.
 */
@NodeInfo(nameTemplate = "FrameGet{p#slotKind/s}{p#frameSlot/s}")
public class FrameGetNode extends FrameAccessNode implements IterableNodeType, Virtualizable, Lowerable {

    public FrameGetNode(Kind kind, ValueNode frame, ValueNode slot, ResolvedJavaField field) {
        super(StampFactory.forKind(kind), kind, frame, slot, field);
    }

    @Override
    public void virtualize(VirtualizerTool tool) {
        if (!isConstantFrameSlot()) {
            return;
        }
        assert isValidAccessKind();
        State virtualFrame = tool.getObjectState(getFrame());
        if (virtualFrame == null || virtualFrame.getState() != EscapeState.Virtual) {
            return;
        }
        assert virtualFrame.getVirtualObject().type() == NewFrameNode.FRAME_TYPE : virtualFrame;
        VirtualInstanceNode virtualFrameObject = (VirtualInstanceNode) virtualFrame.getVirtualObject();
        int arrayFieldIndex = virtualFrameObject.fieldIndex(field);
        State virtualArray = tool.getObjectState(virtualFrame.getEntry(arrayFieldIndex));
        assert virtualArray != null;
        ValueNode result = virtualArray.getEntry(getSlotIndex());
        State virtualResult = tool.getObjectState(result);
        if (virtualResult != null) {
            tool.replaceWithVirtual(virtualResult.getVirtualObject());
        } else {
            tool.replaceWithValue(result);
        }
    }

    @Override
    public void lower(LoweringTool tool) {
        assert !(getFrame() instanceof NewFrameNode);
        StructuredGraph structuredGraph = graph();

        LoadFieldNode loadFieldNode = graph().add(new LoadFieldNode(getFrame(), field));
        structuredGraph.addBeforeFixed(this, loadFieldNode);
        FixedWithNextNode loadNode;
        if (isTagAccess()) {
            ValueNode slotIndex = getSlotOffset(1, tool.getMetaAccess());
            loadNode = graph().add(new LoadIndexedNode(loadFieldNode, slotIndex, getSlotKind()));
        } else if (!getSlotKind().isPrimitive()) {
            ValueNode slotIndex = getSlotOffset(1, tool.getMetaAccess());
            loadNode = graph().add(new LoadIndexedNode(loadFieldNode, slotIndex, Kind.Object));
        } else {
            ValueNode slotOffset = graph().unique(
                            new IntegerAddNode(Kind.Long, getSlotOffset(Unsafe.ARRAY_LONG_INDEX_SCALE, tool.getMetaAccess()), ConstantNode.forLong(Unsafe.ARRAY_LONG_BASE_OFFSET, graph())));
            loadNode = graph().add(new UnsafeLoadNode(loadFieldNode, slotOffset, getSlotKind()));
        }
        structuredGraph.replaceFixedWithFixed(this, loadNode);
        loadFieldNode.lower(tool);
        ((Lowerable) loadNode).lower(tool);
    }

    @NodeIntrinsic
    public static native <T> T get(@ConstantNodeParameter Kind kind, FrameWithoutBoxing frame, FrameSlot slot, @ConstantNodeParameter ResolvedJavaField field);
}