1 /* 2 * Copyright (c) 2016, 2020, 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 24 25 package org.graalvm.compiler.replacements.nodes; 26 27 import java.nio.ByteBuffer; 28 import java.nio.charset.Charset; 29 30 import org.graalvm.compiler.core.common.type.DataPointerConstant; 31 import org.graalvm.compiler.core.common.type.StampFactory; 32 import org.graalvm.compiler.graph.Node.ConstantNodeParameter; 33 import org.graalvm.compiler.graph.Node.NodeIntrinsic; 34 import org.graalvm.compiler.graph.Node.NodeIntrinsicFactory; 35 import org.graalvm.compiler.nodes.ConstantNode; 36 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext; 37 import org.graalvm.compiler.word.Word; 38 39 import jdk.vm.ci.meta.JavaKind; 40 41 /** 42 * Represents a compile-time constant zero-terminated UTF-8 string installed with the generated 43 * code. 44 */ 45 @NodeIntrinsicFactory 46 public final class CStringConstant extends DataPointerConstant { 47 48 private static final Charset UTF8 = Charset.forName("utf8"); 49 50 private final String string; 51 CStringConstant(String string)52 public CStringConstant(String string) { 53 super(1); 54 assert string != null; 55 this.string = string; 56 } 57 58 @Override getSerializedSize()59 public int getSerializedSize() { 60 return string.getBytes(UTF8).length + 1; 61 } 62 63 @Override serialize(ByteBuffer buffer)64 public void serialize(ByteBuffer buffer) { 65 byte[] bytes = string.getBytes(UTF8); 66 buffer.put(bytes); 67 buffer.put((byte) 0); 68 } 69 70 @Override toValueString()71 public String toValueString() { 72 return "c\"" + string + "\""; 73 } 74 intrinsify(GraphBuilderContext b, String string)75 public static boolean intrinsify(GraphBuilderContext b, String string) { 76 b.addPush(JavaKind.Object, new ConstantNode(new CStringConstant(string), StampFactory.pointer())); 77 return true; 78 } 79 80 @NodeIntrinsic cstring(@onstantNodeParameter String string)81 public static native Word cstring(@ConstantNodeParameter String string); 82 } 83