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