Mercurial > hg > truffle
view graal/com.oracle.max.cri/src/com/oracle/max/cri/ci/CiAddress.java @ 4292:48cbd78fa964
First draft of SSA-based linear scan allocator
author | Christian Wimmer <Christian.Wimmer@Oracle.com> |
---|---|
date | Tue, 17 Jan 2012 19:38:48 -0800 |
parents | 430b5db3e6f8 |
children | 9ce8594bedaf |
line wrap: on
line source
/* * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.oracle.max.cri.ci; import static com.oracle.max.cri.ci.CiValueUtil.*; /** * Represents an address in target machine memory, specified via some combination of a base register, an index register, * a displacement and a scale. Note that the base and index registers may be {@link CiVariable variable}, that is as yet * unassigned to target machine registers. */ public final class CiAddress extends CiValue { private static final long serialVersionUID = -1003772042519945089L; /** * A sentinel value used as a place holder in an instruction stream for an address that will be patched. */ public static final CiAddress Placeholder = new CiAddress(CiKind.Illegal, CiValue.IllegalValue); /** * Base register that defines the start of the address computation; always present. */ public final CiValue base; /** * Optional index register, the value of which (possibly scaled by {@link #scale}) is added to {@link #base}. * If not present, is denoted by {@link CiValue#IllegalValue}. */ public final CiValue index; /** * Scaling factor for indexing, dependent on target operand size. */ public final Scale scale; /** * Optional additive displacement. */ public final int displacement; /** * Creates a {@code CiAddress} with given base register, no scaling and no displacement. * @param kind the kind of the value being addressed * @param base the base register */ public CiAddress(CiKind kind, CiValue base) { this(kind, base, IllegalValue, Scale.Times1, 0); } /** * Creates a {@code CiAddress} with given base register, no scaling and a given displacement. * @param kind the kind of the value being addressed * @param base the base register * @param displacement the displacement */ public CiAddress(CiKind kind, CiValue base, int displacement) { this(kind, base, IllegalValue, Scale.Times1, displacement); } /** * Creates a {@code CiAddress} with given base and offset registers, no scaling and no displacement. * @param kind the kind of the value being addressed * @param base the base register * @param offset the offset register */ public CiAddress(CiKind kind, CiValue base, CiValue offset) { this(kind, base, offset, Scale.Times1, 0); } /** * Creates a {@code CiAddress} with given base and index registers, scaling and displacement. * This is the most general constructor.. * @param kind the kind of the value being addressed * @param base the base register * @param index the index register * @param scale the scaling factor * @param displacement the displacement */ public CiAddress(CiKind kind, CiValue base, CiValue index, Scale scale, int displacement) { super(kind); this.base = base; if (isConstant(index)) { long longIndex = ((CiConstant) index).asLong(); long longDisp = displacement + longIndex * scale.value; if ((int) longIndex != longIndex || (int) longDisp != longDisp) { throw new Error("integer overflow when computing constant displacement"); } this.displacement = (int) longDisp; this.index = IllegalValue; this.scale = Scale.Times1; } else { this.index = index; this.scale = scale; this.displacement = displacement; } } /** * A scaling factor used in complex addressing modes such as those supported by x86 platforms. */ public enum Scale { Times1(1, 0), Times2(2, 1), Times4(4, 2), Times8(8, 3); Scale(int value, int log2) { this.value = value; this.log2 = log2; } /** * The value (or multiplier) of this scale. */ public final int value; /** * The {@linkplain #value value} of this scale log 2. */ public final int log2; public static Scale fromInt(int scale) { // Checkstyle: stop switch (scale) { case 1: return Times1; case 2: return Times2; case 4: return Times4; case 8: return Times8; default: throw new IllegalArgumentException(String.valueOf(scale)); } // Checkstyle: resume } public static Scale fromShift(int shift) { return fromInt(1 << shift); } } /** * Encodes the possible addressing modes as a simple value. */ public enum Format { BASE, BASE_DISP, BASE_INDEX, BASE_INDEX_DISP, PLACEHOLDER; } /** * Returns the {@link Format encoded addressing mode} that this {@code CiAddress} represents. * @return the encoded addressing mode */ public Format format() { if (this == Placeholder) { return Format.PLACEHOLDER; } assert isLegal(base); if (isLegal(index)) { if (displacement != 0) { return Format.BASE_INDEX_DISP; } else { return Format.BASE_INDEX; } } else { if (displacement != 0) { return Format.BASE_DISP; } else { return Format.BASE; } } } private static String signed(int i) { if (i >= 0) { return "+" + i; } return String.valueOf(i); } @Override public String toString() { // Checkstyle: stop switch (format()) { case BASE : return "[" + base + kindSuffix() + "]"; case BASE_DISP : return "[" + base + signed(displacement) + kindSuffix() + "]"; case BASE_INDEX : return "[" + base + "+" + index + kindSuffix() + "]"; case BASE_INDEX_DISP : return "[" + base + "+(" + index + "*" + scale.value + ")" + signed(displacement) + kindSuffix() + "]"; case PLACEHOLDER : return "[<placeholder>]"; default : throw new IllegalArgumentException("unknown format: " + format()); } // Checkstyle: resume } @Override public boolean equals(Object obj) { if (obj instanceof CiAddress) { CiAddress addr = (CiAddress) obj; return kind == addr.kind && displacement == addr.displacement && base.equals(addr.base) && scale == addr.scale && index.equals(addr.index); } return false; } @Override public int hashCode() { return (base.hashCode() << 4) | kind.ordinal(); } }