001/*
002 * Copyright (c) 2009, 2011, 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.java;
024
025import jdk.internal.jvmci.meta.*;
026
027import com.oracle.graal.compiler.common.type.*;
028import com.oracle.graal.graph.*;
029import com.oracle.graal.nodeinfo.*;
030import com.oracle.graal.nodes.*;
031import com.oracle.graal.nodes.spi.*;
032
033/**
034 * The base class of all instructions that access fields.
035 */
036@NodeInfo
037public abstract class AccessFieldNode extends FixedWithNextNode implements Lowerable {
038
039    public static final NodeClass<AccessFieldNode> TYPE = NodeClass.create(AccessFieldNode.class);
040    @OptionalInput ValueNode object;
041
042    protected final ResolvedJavaField field;
043
044    public ValueNode object() {
045        return object;
046    }
047
048    /**
049     * Constructs a new access field object.
050     *
051     * @param object the instruction producing the receiver object
052     * @param field the compiler interface representation of the field
053     */
054    public AccessFieldNode(NodeClass<? extends AccessFieldNode> c, Stamp stamp, ValueNode object, ResolvedJavaField field) {
055        super(c, stamp);
056        this.object = object;
057        this.field = field;
058        assert field.getDeclaringClass().isInitialized();
059    }
060
061    /**
062     * Gets the compiler interface field for this field access.
063     *
064     * @return the compiler interface field for this field access
065     */
066    public ResolvedJavaField field() {
067        return field;
068    }
069
070    /**
071     * Checks whether this field access is an access to a static field.
072     *
073     * @return {@code true} if this field access is to a static field
074     */
075    public boolean isStatic() {
076        return field.isStatic();
077    }
078
079    /**
080     * Checks whether this field is declared volatile.
081     *
082     * @return {@code true} if the field is resolved and declared volatile
083     */
084    public boolean isVolatile() {
085        return field.isVolatile();
086    }
087
088    @Override
089    public void lower(LoweringTool tool) {
090        tool.getLowerer().lower(this, tool);
091    }
092
093    @Override
094    public String toString(Verbosity verbosity) {
095        if (verbosity == Verbosity.Name) {
096            return super.toString(verbosity) + "#" + field.getName();
097        } else {
098            return super.toString(verbosity);
099        }
100    }
101
102    @Override
103    public boolean verify() {
104        assertTrue((object == null) == isStatic(), "static field must not have object, instance field must have object");
105        return super.verify();
106    }
107}