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