001/*
002 * Copyright (c) 2011, 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.virtual;
024
025import java.nio.*;
026
027import jdk.internal.jvmci.meta.*;
028import sun.misc.*;
029
030import com.oracle.graal.graph.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.spi.*;
034
035@NodeInfo(nameTemplate = "VirtualArray {p#componentType/s}[{p#length}]")
036public class VirtualArrayNode extends VirtualObjectNode implements ArrayLengthProvider {
037
038    public static final NodeClass<VirtualArrayNode> TYPE = NodeClass.create(VirtualArrayNode.class);
039    protected final ResolvedJavaType componentType;
040    protected final int length;
041
042    public VirtualArrayNode(ResolvedJavaType componentType, int length) {
043        this(TYPE, componentType, length);
044    }
045
046    protected VirtualArrayNode(NodeClass<? extends VirtualObjectNode> c, ResolvedJavaType componentType, int length) {
047        super(c, componentType.getArrayClass(), true);
048        this.componentType = componentType;
049        this.length = length;
050    }
051
052    @Override
053    public ResolvedJavaType type() {
054        return componentType.getArrayClass();
055    }
056
057    public ResolvedJavaType componentType() {
058        return componentType;
059    }
060
061    @Override
062    public int entryCount() {
063        return length;
064    }
065
066    @Override
067    public void generate(NodeLIRBuilderTool gen) {
068        // nothing to do...
069    }
070
071    @Override
072    public String toString(Verbosity verbosity) {
073        if (verbosity == Verbosity.Name) {
074            return super.toString(Verbosity.Name) + " " + componentType.getName() + "[" + length + "]";
075        } else {
076            return super.toString(verbosity);
077        }
078    }
079
080    @Override
081    public String entryName(int index) {
082        return "[" + index + "]";
083    }
084
085    @Override
086    public int entryIndexForOffset(long constantOffset, Kind expectedEntryKind) {
087        return entryIndexForOffset(constantOffset, expectedEntryKind, componentType, length);
088    }
089
090    public static int entryIndexForOffset(long constantOffset, Kind expectedEntryKind, ResolvedJavaType componentType, int length) {
091        int baseOffset;
092        int indexScale;
093        switch (componentType.getKind()) {
094            case Boolean:
095                baseOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
096                indexScale = Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
097                break;
098            case Byte:
099                baseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
100                indexScale = Unsafe.ARRAY_BYTE_INDEX_SCALE;
101                break;
102            case Short:
103                baseOffset = Unsafe.ARRAY_SHORT_BASE_OFFSET;
104                indexScale = Unsafe.ARRAY_SHORT_INDEX_SCALE;
105                break;
106            case Char:
107                baseOffset = Unsafe.ARRAY_CHAR_BASE_OFFSET;
108                indexScale = Unsafe.ARRAY_CHAR_INDEX_SCALE;
109                break;
110            case Int:
111                baseOffset = Unsafe.ARRAY_INT_BASE_OFFSET;
112                indexScale = Unsafe.ARRAY_INT_INDEX_SCALE;
113                break;
114            case Long:
115                baseOffset = Unsafe.ARRAY_LONG_BASE_OFFSET;
116                indexScale = Unsafe.ARRAY_LONG_INDEX_SCALE;
117                break;
118            case Float:
119                baseOffset = Unsafe.ARRAY_FLOAT_BASE_OFFSET;
120                indexScale = Unsafe.ARRAY_FLOAT_INDEX_SCALE;
121                break;
122            case Double:
123                baseOffset = Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
124                indexScale = Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
125                break;
126            case Object:
127                baseOffset = Unsafe.ARRAY_OBJECT_BASE_OFFSET;
128                indexScale = Unsafe.ARRAY_OBJECT_INDEX_SCALE;
129                break;
130            default:
131                return -1;
132        }
133        long offset;
134        if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN && componentType.isPrimitive()) {
135            // On big endian, we do just get expect the type be right aligned in this memory slot
136            offset = constantOffset - (componentType.getKind().getByteCount() - Math.min(componentType.getKind().getByteCount(), 4 + expectedEntryKind.getByteCount()));
137        } else {
138            offset = constantOffset;
139        }
140        long index = offset - baseOffset;
141        if (index % indexScale != 0) {
142            return -1;
143        }
144        long elementIndex = index / indexScale;
145        if (elementIndex < 0 || elementIndex >= length) {
146            return -1;
147        }
148        return (int) elementIndex;
149    }
150
151    @Override
152    public Kind entryKind(int index) {
153        assert index >= 0 && index < length;
154        return componentType.getKind();
155    }
156
157    @Override
158    public VirtualArrayNode duplicate() {
159        return new VirtualArrayNode(componentType, length);
160    }
161
162    @Override
163    public ValueNode getMaterializedRepresentation(FixedNode fixed, ValueNode[] entries, LockState locks) {
164        return new AllocatedObjectNode(this);
165    }
166
167    public ValueNode length() {
168        return ConstantNode.forInt(length);
169    }
170}