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