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 jdk.internal.jvmci.hotspot;
024
025import java.util.*;
026
027import jdk.internal.jvmci.hotspot.HotSpotVMConfig.*;
028import jdk.internal.jvmci.meta.*;
029
030public final class HotSpotMetaspaceConstantImpl extends PrimitiveConstant implements HotSpotMetaspaceConstant, VMConstant, HotSpotProxified {
031
032    static HotSpotMetaspaceConstantImpl forMetaspaceObject(Kind kind, long primitive, Object metaspaceObject, boolean compressed) {
033        return new HotSpotMetaspaceConstantImpl(kind, primitive, metaspaceObject, compressed);
034    }
035
036    static Object getMetaspaceObject(Constant constant) {
037        return ((HotSpotMetaspaceConstantImpl) constant).metaspaceObject;
038    }
039
040    private final Object metaspaceObject;
041    private final boolean compressed;
042
043    private HotSpotMetaspaceConstantImpl(Kind kind, long primitive, Object metaspaceObject, boolean compressed) {
044        super(kind, primitive);
045        this.metaspaceObject = metaspaceObject;
046        this.compressed = compressed;
047    }
048
049    @Override
050    public int hashCode() {
051        return super.hashCode() ^ System.identityHashCode(metaspaceObject);
052    }
053
054    @Override
055    public boolean equals(Object o) {
056        return o == this || (o instanceof HotSpotMetaspaceConstantImpl && super.equals(o) && Objects.equals(metaspaceObject, ((HotSpotMetaspaceConstantImpl) o).metaspaceObject));
057    }
058
059    @Override
060    public String toString() {
061        return super.toString() + "{" + metaspaceObject + (compressed ? ";compressed}" : "}");
062    }
063
064    public boolean isCompressed() {
065        return compressed;
066    }
067
068    public JavaConstant compress(CompressEncoding encoding) {
069        assert !isCompressed();
070        HotSpotMetaspaceConstantImpl res = HotSpotMetaspaceConstantImpl.forMetaspaceObject(Kind.Int, encoding.compress(asLong()), metaspaceObject, true);
071        assert res.isCompressed();
072        return res;
073    }
074
075    public JavaConstant uncompress(CompressEncoding encoding) {
076        assert isCompressed();
077        HotSpotMetaspaceConstantImpl res = HotSpotMetaspaceConstantImpl.forMetaspaceObject(Kind.Long, encoding.uncompress(asInt()), metaspaceObject, false);
078        assert !res.isCompressed();
079        return res;
080    }
081
082    public HotSpotResolvedObjectType asResolvedJavaType() {
083        if (metaspaceObject instanceof HotSpotResolvedObjectType) {
084            return (HotSpotResolvedObjectType) metaspaceObject;
085        }
086        return null;
087    }
088
089    public long rawValue() {
090        return asLong();
091    }
092}