001/*
002 * Copyright (c) 2015, 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 */
023
024package com.oracle.graal.compiler.amd64;
025
026import jdk.internal.jvmci.meta.*;
027
028import com.oracle.graal.asm.amd64.AMD64Address.Scale;
029import com.oracle.graal.graph.*;
030import com.oracle.graal.lir.amd64.*;
031import com.oracle.graal.nodeinfo.*;
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.memory.address.*;
034import com.oracle.graal.nodes.spi.*;
035
036/**
037 * Represents an address of the form [base + index*scale + displacement]. Both base and index are
038 * optional.
039 */
040@NodeInfo
041public class AMD64AddressNode extends AddressNode implements LIRLowerable {
042
043    public static final NodeClass<AMD64AddressNode> TYPE = NodeClass.create(AMD64AddressNode.class);
044
045    @OptionalInput private ValueNode base;
046
047    @OptionalInput private ValueNode index;
048    private Scale scale;
049
050    private int displacement;
051
052    public AMD64AddressNode(ValueNode base) {
053        this(base, null);
054    }
055
056    public AMD64AddressNode(ValueNode base, ValueNode index) {
057        super(TYPE);
058        this.base = base;
059        this.index = index;
060        this.scale = Scale.Times1;
061    }
062
063    public void generate(NodeLIRBuilderTool gen) {
064        AMD64LIRGenerator tool = (AMD64LIRGenerator) gen.getLIRGeneratorTool();
065
066        AllocatableValue baseValue = base == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(base));
067        AllocatableValue indexValue = index == null ? Value.ILLEGAL : tool.asAllocatable(gen.operand(index));
068
069        AllocatableValue baseReference = LIRKind.derivedBaseFromValue(baseValue);
070        AllocatableValue indexReference;
071        if (scale.equals(Scale.Times1)) {
072            indexReference = LIRKind.derivedBaseFromValue(indexValue);
073        } else {
074            if (indexValue.getLIRKind().isValue()) {
075                indexReference = null;
076            } else {
077                indexReference = Value.ILLEGAL;
078            }
079        }
080
081        LIRKind kind = LIRKind.combineDerived(tool.getLIRKind(stamp()), baseReference, indexReference);
082        gen.setResult(this, new AMD64AddressValue(kind, baseValue, indexValue, scale, displacement));
083    }
084
085    public ValueNode getBase() {
086        return base;
087    }
088
089    public void setBase(ValueNode base) {
090        // allow modification before inserting into the graph
091        if (isAlive()) {
092            updateUsages(this.base, base);
093        }
094        this.base = base;
095    }
096
097    public ValueNode getIndex() {
098        return index;
099    }
100
101    public void setIndex(ValueNode index) {
102        // allow modification before inserting into the graph
103        if (isAlive()) {
104            updateUsages(this.index, index);
105        }
106        this.index = index;
107    }
108
109    public Scale getScale() {
110        return scale;
111    }
112
113    public void setScale(Scale scale) {
114        this.scale = scale;
115    }
116
117    public int getDisplacement() {
118        return displacement;
119    }
120
121    public void setDisplacement(int displacement) {
122        this.displacement = displacement;
123    }
124}