001/*
002 * Copyright (c) 2014, 2014, 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.hotspot.nodes.type;
024
025import jdk.internal.jvmci.hotspot.*;
026import jdk.internal.jvmci.hotspot.HotSpotVMConfig.*;
027import jdk.internal.jvmci.meta.*;
028
029import com.oracle.graal.compiler.common.spi.*;
030import com.oracle.graal.compiler.common.type.*;
031
032public class NarrowOopStamp extends AbstractObjectStamp {
033
034    private final CompressEncoding encoding;
035
036    public NarrowOopStamp(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull, CompressEncoding encoding) {
037        super(type, exactType, nonNull, alwaysNull);
038        this.encoding = encoding;
039    }
040
041    @Override
042    protected AbstractObjectStamp copyWith(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
043        return new NarrowOopStamp(type, exactType, nonNull, alwaysNull, encoding);
044    }
045
046    public static Stamp compressed(AbstractObjectStamp stamp, CompressEncoding encoding) {
047        return new NarrowOopStamp(stamp.type(), stamp.isExactType(), stamp.nonNull(), stamp.alwaysNull(), encoding);
048    }
049
050    public Stamp uncompressed() {
051        return new ObjectStamp(type(), isExactType(), nonNull(), alwaysNull());
052    }
053
054    public CompressEncoding getEncoding() {
055        return encoding;
056    }
057
058    @Override
059    public LIRKind getLIRKind(LIRKindTool tool) {
060        return LIRKind.reference(Kind.Int);
061    }
062
063    @Override
064    public String toString() {
065        StringBuilder str = new StringBuilder();
066        str.append('n');
067        appendString(str);
068        return str.toString();
069    }
070
071    @Override
072    public boolean isCompatible(Stamp other) {
073        if (this == other) {
074            return true;
075        }
076        if (other instanceof NarrowOopStamp) {
077            NarrowOopStamp narrow = (NarrowOopStamp) other;
078            return encoding.equals(narrow.encoding);
079        }
080        return false;
081    }
082
083    @Override
084    public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
085        HotSpotMemoryAccessProvider hsProvider = (HotSpotMemoryAccessProvider) provider;
086        return hsProvider.readNarrowOopConstant(base, displacement, encoding);
087    }
088
089    @Override
090    public int hashCode() {
091        final int prime = 31;
092        int result = super.hashCode();
093        result = prime * result + encoding.hashCode();
094        return result;
095    }
096
097    @Override
098    public boolean equals(Object obj) {
099        if (this == obj) {
100            return true;
101        }
102        if (obj == null || getClass() != obj.getClass()) {
103            return false;
104        }
105        NarrowOopStamp other = (NarrowOopStamp) obj;
106        if (!encoding.equals(other.encoding)) {
107            return false;
108        }
109        return super.equals(other);
110    }
111
112    @Override
113    public JavaConstant asConstant() {
114        if (alwaysNull()) {
115            return HotSpotCompressedNullConstant.COMPRESSED_NULL;
116        } else {
117            return null;
118        }
119    }
120}