1 /*
   2  * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.nodes.virtual;
  26 
  27 import org.graalvm.compiler.core.common.spi.ArrayOffsetProvider;
  28 import org.graalvm.compiler.graph.NodeClass;
  29 import org.graalvm.compiler.nodeinfo.NodeInfo;
  30 import org.graalvm.compiler.nodeinfo.Verbosity;
  31 import org.graalvm.compiler.nodes.FixedNode;
  32 import org.graalvm.compiler.nodes.ValueNode;
  33 
  34 import jdk.vm.ci.meta.JavaKind;
  35 import jdk.vm.ci.meta.ResolvedJavaField;
  36 import jdk.vm.ci.meta.ResolvedJavaType;
  37 
  38 @NodeInfo(nameTemplate = "VirtualInstance({p#objectId}) {p#type/s}")
  39 public class VirtualInstanceNode extends VirtualObjectNode {
  40 
  41     public static final NodeClass<VirtualInstanceNode> TYPE = NodeClass.create(VirtualInstanceNode.class);
  42     protected final ResolvedJavaType type;
  43     protected final ResolvedJavaField[] fields;
  44 
  45     public VirtualInstanceNode(ResolvedJavaType type, boolean hasIdentity) {
  46         this(type, type.getInstanceFields(true), hasIdentity);
  47     }
  48 
  49     public VirtualInstanceNode(ResolvedJavaType type, ResolvedJavaField[] fields, boolean hasIdentity) {
  50         this(TYPE, type, fields, hasIdentity);
  51     }
  52 
  53     protected VirtualInstanceNode(NodeClass<? extends VirtualInstanceNode> c, ResolvedJavaType type, boolean hasIdentity) {
  54         this(c, type, type.getInstanceFields(true), hasIdentity);
  55     }
  56 
  57     protected VirtualInstanceNode(NodeClass<? extends VirtualInstanceNode> c, ResolvedJavaType type, ResolvedJavaField[] fields, boolean hasIdentity) {
  58         super(c, type, hasIdentity);
  59         this.type = type;
  60         this.fields = fields;
  61     }
  62 
  63     @Override
  64     public ResolvedJavaType type() {
  65         return type;
  66     }
  67 
  68     @Override
  69     public int entryCount() {
  70         return fields.length;
  71     }
  72 
  73     public ResolvedJavaField field(int index) {
  74         return fields[index];
  75     }
  76 
  77     public ResolvedJavaField[] getFields() {
  78         return fields;
  79     }
  80 
  81     @Override
  82     public String toString(Verbosity verbosity) {
  83         if (verbosity == Verbosity.Name) {
  84             return super.toString(Verbosity.Name) + "(" + getObjectId() + ") " + type.toJavaName(false);
  85         } else {
  86             return super.toString(verbosity);
  87         }
  88     }
  89 
  90     @Override
  91     public String entryName(int index) {
  92         return fields[index].getName();
  93     }
  94 
  95     public int fieldIndex(ResolvedJavaField field) {
  96         // on average fields.length == ~6, so a linear search is fast enough
  97         for (int i = 0; i < fields.length; i++) {
  98             if (fields[i].equals(field)) {
  99                 return i;
 100             }
 101         }
 102         return -1;
 103     }
 104 
 105     @Override
 106     public int entryIndexForOffset(ArrayOffsetProvider arrayOffsetProvider, long constantOffset, JavaKind expectedEntryKind) {
 107         return fieldIndex(type.findInstanceFieldWithOffset(constantOffset, expectedEntryKind));
 108     }
 109 
 110     @Override
 111     public JavaKind entryKind(int index) {
 112         assert index >= 0 && index < fields.length;
 113         return fields[index].getJavaKind();
 114     }
 115 
 116     @Override
 117     public VirtualInstanceNode duplicate() {
 118         VirtualInstanceNode node = new VirtualInstanceNode(type, fields, super.hasIdentity());
 119         node.setNodeSourcePosition(this.getNodeSourcePosition());
 120         return node;
 121     }
 122 
 123     @Override
 124     public ValueNode getMaterializedRepresentation(FixedNode fixed, ValueNode[] entries, LockState locks) {
 125         AllocatedObjectNode node = new AllocatedObjectNode(this);
 126         node.setNodeSourcePosition(this.getNodeSourcePosition());
 127         return node;
 128     }
 129 }