1 /*
   2  * Copyright (c) 2009, 2011, 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 package org.graalvm.compiler.nodes.java;
  24 
  25 import static org.graalvm.compiler.nodeinfo.NodeCycles.CYCLES_15;
  26 import static org.graalvm.compiler.nodeinfo.NodeSize.SIZE_10;
  27 
  28 import org.graalvm.compiler.core.common.type.Stamp;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.nodeinfo.NodeInfo;
  31 import org.graalvm.compiler.nodeinfo.Verbosity;
  32 import org.graalvm.compiler.nodes.FixedWithNextNode;
  33 import org.graalvm.compiler.nodes.ValueNode;
  34 import org.graalvm.compiler.nodes.spi.Lowerable;
  35 import org.graalvm.compiler.nodes.spi.LoweringTool;
  36 
  37 import jdk.vm.ci.meta.ResolvedJavaField;
  38 
  39 /**
  40  * The base class of all instructions that access fields.
  41  */
  42 @NodeInfo(cycles = CYCLES_15, size = SIZE_10)
  43 public abstract class AccessFieldNode extends FixedWithNextNode implements Lowerable {
  44 
  45     public static final NodeClass<AccessFieldNode> TYPE = NodeClass.create(AccessFieldNode.class);
  46     @OptionalInput ValueNode object;
  47 
  48     protected final ResolvedJavaField field;
  49 
  50     public ValueNode object() {
  51         return object;
  52     }
  53 
  54     /**
  55      * Constructs a new access field object.
  56      *
  57      * @param object the instruction producing the receiver object
  58      * @param field the compiler interface representation of the field
  59      */
  60     public AccessFieldNode(NodeClass<? extends AccessFieldNode> c, Stamp stamp, ValueNode object, ResolvedJavaField field) {
  61         super(c, stamp);
  62         this.object = object;
  63         this.field = field;
  64         assert field.getDeclaringClass().isInitialized();
  65     }
  66 
  67     /**
  68      * Gets the compiler interface field for this field access.
  69      *
  70      * @return the compiler interface field for this field access
  71      */
  72     public ResolvedJavaField field() {
  73         return field;
  74     }
  75 
  76     /**
  77      * Checks whether this field access is an access to a static field.
  78      *
  79      * @return {@code true} if this field access is to a static field
  80      */
  81     public boolean isStatic() {
  82         return field.isStatic();
  83     }
  84 
  85     /**
  86      * Checks whether this field is declared volatile.
  87      *
  88      * @return {@code true} if the field is resolved and declared volatile
  89      */
  90     public boolean isVolatile() {
  91         return field.isVolatile();
  92     }
  93 
  94     @Override
  95     public void lower(LoweringTool tool) {
  96         tool.getLowerer().lower(this, tool);
  97     }
  98 
  99     @Override
 100     public String toString(Verbosity verbosity) {
 101         if (verbosity == Verbosity.Name) {
 102             return super.toString(verbosity) + "#" + field.getName();
 103         } else {
 104             return super.toString(verbosity);
 105         }
 106     }
 107 
 108     @Override
 109     public boolean verify() {
 110         assertTrue((object == null) == isStatic(), "static field must not have object, instance field must have object");
 111         return super.verify();
 112     }
 113 }