1 /* 2 * Copyright (c) 2012, 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.hotspot.replacements; 26 27 import static org.graalvm.compiler.hotspot.replacements.UnsafeAccess.UNSAFE; 28 import static org.graalvm.compiler.replacements.SnippetTemplate.DEFAULT_REPLACER; 29 30 import org.graalvm.compiler.api.replacements.Fold; 31 import org.graalvm.compiler.api.replacements.Snippet; 32 import org.graalvm.compiler.api.replacements.Snippet.ConstantParameter; 33 import org.graalvm.compiler.debug.DebugHandlersFactory; 34 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 35 import org.graalvm.compiler.nodes.NamedLocationIdentity; 36 import org.graalvm.compiler.nodes.debug.StringToBytesNode; 37 import org.graalvm.compiler.nodes.java.NewArrayNode; 38 import org.graalvm.compiler.nodes.spi.LoweringTool; 39 import org.graalvm.compiler.options.OptionValues; 40 import org.graalvm.compiler.replacements.SnippetTemplate; 41 import org.graalvm.compiler.replacements.SnippetTemplate.AbstractTemplates; 42 import org.graalvm.compiler.replacements.SnippetTemplate.Arguments; 43 import org.graalvm.compiler.replacements.SnippetTemplate.SnippetInfo; 44 import org.graalvm.compiler.replacements.Snippets; 45 import org.graalvm.compiler.replacements.nodes.CStringConstant; 46 import org.graalvm.compiler.word.Word; 47 import jdk.internal.vm.compiler.word.LocationIdentity; 48 49 import jdk.vm.ci.code.TargetDescription; 50 import jdk.vm.ci.meta.JavaKind; 51 52 /** 53 * The {@code StringToBytesSnippets} contains a snippet for lowering {@link StringToBytesNode}. 54 */ 55 public class StringToBytesSnippets implements Snippets { 56 57 public static final LocationIdentity CSTRING_LOCATION = NamedLocationIdentity.immutable("CString location"); 58 59 @Fold 60 static long arrayBaseOffset() { 61 return UNSAFE.arrayBaseOffset(char[].class); 62 } 63 64 @Snippet 65 public static byte[] transform(@ConstantParameter String compilationTimeString) { 66 int i = compilationTimeString.length(); 67 byte[] array = (byte[]) NewArrayNode.newUninitializedArray(byte.class, i); 68 Word cArray = CStringConstant.cstring(compilationTimeString); 69 while (i-- > 0) { 70 // array[i] = cArray.readByte(i); 71 UNSAFE.putByte(array, arrayBaseOffset() + i, cArray.readByte(i, CSTRING_LOCATION)); 72 } 73 return array; 74 } 75 76 public static class Templates extends AbstractTemplates { 77 78 private final SnippetInfo create; 79 80 public Templates(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, TargetDescription target) { 81 super(options, factories, providers, providers.getSnippetReflection(), target); 82 create = snippet(StringToBytesSnippets.class, "transform", NamedLocationIdentity.getArrayLocation(JavaKind.Byte)); 83 } 84 85 public void lower(StringToBytesNode stringToBytesNode, LoweringTool tool) { 86 Arguments args = new Arguments(create, stringToBytesNode.graph().getGuardsStage(), tool.getLoweringStage()); 87 args.addConst("compilationTimeString", stringToBytesNode.getValue()); 88 SnippetTemplate template = template(stringToBytesNode, args); 89 template.instantiate(providers.getMetaAccess(), stringToBytesNode, DEFAULT_REPLACER, args); 90 } 91 92 } 93 94 }