1 /*
   2  * Copyright (c) 2009, 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 package org.graalvm.compiler.nodes.java;
  24 
  25 import org.graalvm.compiler.core.common.type.StampFactory;
  26 import org.graalvm.compiler.graph.NodeClass;
  27 import org.graalvm.compiler.nodeinfo.InputType;
  28 import org.graalvm.compiler.nodeinfo.NodeInfo;
  29 import org.graalvm.compiler.nodes.FrameState;
  30 import org.graalvm.compiler.nodes.StateSplit;
  31 import org.graalvm.compiler.nodes.ValueNode;
  32 import org.graalvm.compiler.nodes.spi.Virtualizable;
  33 import org.graalvm.compiler.nodes.spi.VirtualizerTool;
  34 import org.graalvm.compiler.nodes.virtual.VirtualInstanceNode;
  35 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode;
  36 
  37 import jdk.vm.ci.meta.ResolvedJavaField;
  38 
  39 /**
  40  * The {@code StoreFieldNode} represents a write to a static or instance field.
  41  */
  42 @NodeInfo(nameTemplate = "StoreField#{p#field/s}")
  43 public final class StoreFieldNode extends AccessFieldNode implements StateSplit, Virtualizable {
  44     public static final NodeClass<StoreFieldNode> TYPE = NodeClass.create(StoreFieldNode.class);
  45 
  46     @Input ValueNode value;
  47     @OptionalInput(InputType.State) FrameState stateAfter;
  48 
  49     @Override
  50     public FrameState stateAfter() {
  51         return stateAfter;
  52     }
  53 
  54     @Override
  55     public void setStateAfter(FrameState x) {
  56         assert x == null || x.isAlive() : "frame state must be in a graph";
  57         updateUsages(stateAfter, x);
  58         stateAfter = x;
  59     }
  60 
  61     @Override
  62     public boolean hasSideEffect() {
  63         return true;
  64     }
  65 
  66     public ValueNode value() {
  67         return value;
  68     }
  69 
  70     public StoreFieldNode(ValueNode object, ResolvedJavaField field, ValueNode value) {
  71         super(TYPE, StampFactory.forVoid(), object, field);
  72         this.value = value;
  73     }
  74 
  75     public StoreFieldNode(ValueNode object, ResolvedJavaField field, ValueNode value, FrameState stateAfter) {
  76         super(TYPE, StampFactory.forVoid(), object, field);
  77         this.value = value;
  78         this.stateAfter = stateAfter;
  79     }
  80 
  81     @Override
  82     public void virtualize(VirtualizerTool tool) {
  83         ValueNode alias = tool.getAlias(object());
  84         if (alias instanceof VirtualObjectNode) {
  85             VirtualInstanceNode virtual = (VirtualInstanceNode) alias;
  86             int fieldIndex = virtual.fieldIndex(field());
  87             if (fieldIndex != -1) {
  88                 tool.setVirtualEntry(virtual, fieldIndex, value(), false);
  89                 tool.delete();
  90             }
  91         }
  92     }
  93 
  94     public FrameState getState() {
  95         return stateAfter;
  96     }
  97 }