1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.hotspot.replacements;
  24 
  25 import java.io.ByteArrayOutputStream;
  26 import java.io.DataOutputStream;
  27 import java.nio.ByteBuffer;
  28 
  29 import org.graalvm.compiler.common.PermanentBailoutException;
  30 import org.graalvm.compiler.core.common.type.DataPointerConstant;
  31 
  32 import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant;
  33 import jdk.vm.ci.hotspot.HotSpotObjectConstant;
  34 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  35 import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  36 import jdk.vm.ci.meta.Constant;
  37 
  38 /**
  39  * Represents an encoded representation of a constant.
  40  */
  41 public final class EncodedSymbolConstant extends DataPointerConstant {
  42     private final Constant constant;
  43     private byte[] bytes;
  44 
  45     public EncodedSymbolConstant(Constant constant) {
  46         super(1);
  47         this.constant = constant;
  48     }
  49 
  50     @Override
  51     public int getSerializedSize() {
  52         return getEncodedConstant().length;
  53     }
  54 
  55     @Override
  56     public void serialize(ByteBuffer buffer) {
  57         buffer.put(getEncodedConstant());
  58     }
  59 
  60     /**
  61      * Converts a string to a byte array with modified UTF-8 encoding. The first two bytes of the
  62      * byte array store the length of the string in bytes.
  63      *
  64      * @param s a java.lang.String in UTF-16
  65      */
  66     private static byte[] toUTF8String(String s) {
  67         try (ByteArrayOutputStream bytes = new ByteArrayOutputStream()) {
  68             DataOutputStream stream = new DataOutputStream(bytes);
  69             stream.writeUTF(s);
  70             return bytes.toByteArray();
  71         } catch (Exception e) {
  72             throw new PermanentBailoutException(e, "String conversion failed: %s", s);
  73         }
  74     }
  75 
  76     private static byte[] encodeConstant(Constant constant) {
  77         assert constant != null;
  78         if (constant instanceof HotSpotObjectConstant) {
  79             return toUTF8String(((HotSpotObjectConstant) constant).asObject(String.class));
  80         } else if (constant instanceof HotSpotMetaspaceConstant) {
  81             HotSpotMetaspaceConstant metaspaceConstant = ((HotSpotMetaspaceConstant) constant);
  82             HotSpotResolvedObjectType klass = metaspaceConstant.asResolvedJavaType();
  83             if (klass != null) {
  84                 return toUTF8String(klass.getName());
  85             }
  86             HotSpotResolvedJavaMethod method = metaspaceConstant.asResolvedJavaMethod();
  87             if (method != null) {
  88                 byte[] methodName = toUTF8String(method.getName());
  89                 byte[] signature = toUTF8String(method.getSignature().toMethodDescriptor());
  90                 byte[] result = new byte[methodName.length + signature.length];
  91                 int resultPos = 0;
  92                 System.arraycopy(methodName, 0, result, resultPos, methodName.length);
  93                 resultPos += methodName.length;
  94                 System.arraycopy(signature, 0, result, resultPos, signature.length);
  95                 resultPos += signature.length;
  96                 assert resultPos == result.length;
  97                 return result;
  98             }
  99 
 100         }
 101         throw new PermanentBailoutException("Encoding of constant %s failed", constant);
 102     }
 103 
 104     public byte[] getEncodedConstant() {
 105         if (bytes == null) {
 106             bytes = encodeConstant(constant);
 107         }
 108         return bytes;
 109     }
 110 
 111     @Override
 112     public String toValueString() {
 113         return "encoded symbol\"" + constant.toValueString() + "\"";
 114     }
 115 
 116 }