Search in sources :

Example 1 with ArrayCopyCallNode

use of org.graalvm.compiler.replacements.arraycopy.ArrayCopyCallNode in project graal by oracle.

the class HotSpotGraphBuilderPlugins method registerStringPlugins.

private static void registerStringPlugins(InvocationPlugins plugins, Replacements replacements, WordTypes wordTypes, ArrayCopyForeignCalls foreignCalls, GraalHotSpotVMConfig vmConfig) {
    final Registration utf16r = new Registration(plugins, "java.lang.StringUTF16", replacements);
    utf16r.register(new InvocationPlugin("toBytes", char[].class, int.class, int.class) {

        private static final int MAX_LENGTH = Integer.MAX_VALUE >> 1;

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value, ValueNode srcBegin, ValueNode length) {
            try (HotSpotInvocationPluginHelper helper = new HotSpotInvocationPluginHelper(b, targetMethod, vmConfig)) {
                helper.intrinsicRangeCheck(srcBegin, Condition.LT, ConstantNode.forInt(0));
                helper.intrinsicRangeCheck(length, Condition.LT, ConstantNode.forInt(0));
                helper.intrinsicRangeCheck(length, Condition.GT, ConstantNode.forInt(MAX_LENGTH));
                ValueNode valueLength = b.add(new ArrayLengthNode(value));
                ValueNode limit = b.add(new SubNode(valueLength, length));
                helper.intrinsicRangeCheck(srcBegin, Condition.GT, limit);
                ValueNode newArray = b.add(new NewArrayNode(b.getMetaAccess().lookupJavaType(Byte.TYPE), b.add(new LeftShiftNode(length, ConstantNode.forInt(1))), false));
                b.addPush(JavaKind.Object, newArray);
                // The stateAfter should include the value pushed, so push it first and then
                // perform the call that fills in the array.
                b.add(new ArrayCopyCallNode(foreignCalls, wordTypes, value, srcBegin, newArray, ConstantNode.forInt(0), length, JavaKind.Char, LocationIdentity.init(), false, true, true, vmConfig.heapWordSize));
            }
            return true;
        }
    });
    utf16r.register(new InvocationPlugin("getChars", byte[].class, int.class, int.class, char[].class, int.class) {

        @Override
        public boolean apply(GraphBuilderContext b, ResolvedJavaMethod targetMethod, Receiver receiver, ValueNode value, ValueNode srcBegin, ValueNode srcEnd, ValueNode dst, ValueNode dstBegin) {
            try (HotSpotInvocationPluginHelper helper = new HotSpotInvocationPluginHelper(b, targetMethod, vmConfig)) {
                ValueNode length = helper.sub(srcEnd, srcBegin);
                helper.intrinsicRangeCheck(srcBegin, Condition.LT, ConstantNode.forInt(0));
                helper.intrinsicRangeCheck(length, Condition.LT, ConstantNode.forInt(0));
                ValueNode srcLimit = helper.sub(helper.shr(helper.length(value), 1), length);
                helper.intrinsicRangeCheck(srcBegin, Condition.GT, srcLimit);
                ValueNode limit = helper.sub(helper.length(dst), length);
                helper.intrinsicRangeCheck(dstBegin, Condition.GT, limit);
                b.add(new ArrayCopyCallNode(foreignCalls, wordTypes, value, srcBegin, dst, dstBegin, length, JavaKind.Char, JavaKind.Byte, JavaKind.Char, false, true, true, vmConfig.heapWordSize));
            }
            return true;
        }
    });
}
Also used : DynamicNewArrayNode(org.graalvm.compiler.nodes.java.DynamicNewArrayNode) NewArrayNode(org.graalvm.compiler.nodes.java.NewArrayNode) Receiver(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver) LeftShiftNode(org.graalvm.compiler.nodes.calc.LeftShiftNode) SubNode(org.graalvm.compiler.nodes.calc.SubNode) GraphBuilderContext(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext) Registration(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration) ValueNode(org.graalvm.compiler.nodes.ValueNode) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) SnippetSubstitutionInvocationPlugin(org.graalvm.compiler.replacements.SnippetSubstitutionInvocationPlugin) InlineOnlyInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.InlineOnlyInvocationPlugin) ArrayCopyCallNode(org.graalvm.compiler.replacements.arraycopy.ArrayCopyCallNode) ArrayLengthNode(org.graalvm.compiler.nodes.java.ArrayLengthNode) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) HotSpotInvocationPluginHelper(org.graalvm.compiler.hotspot.replacements.HotSpotInvocationPluginHelper)

Aggregations

ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)1 HotSpotInvocationPluginHelper (org.graalvm.compiler.hotspot.replacements.HotSpotInvocationPluginHelper)1 ValueNode (org.graalvm.compiler.nodes.ValueNode)1 LeftShiftNode (org.graalvm.compiler.nodes.calc.LeftShiftNode)1 SubNode (org.graalvm.compiler.nodes.calc.SubNode)1 GraphBuilderContext (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderContext)1 InvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin)1 InlineOnlyInvocationPlugin (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.InlineOnlyInvocationPlugin)1 Receiver (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.Receiver)1 Registration (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins.Registration)1 ArrayLengthNode (org.graalvm.compiler.nodes.java.ArrayLengthNode)1 DynamicNewArrayNode (org.graalvm.compiler.nodes.java.DynamicNewArrayNode)1 NewArrayNode (org.graalvm.compiler.nodes.java.NewArrayNode)1 SnippetSubstitutionInvocationPlugin (org.graalvm.compiler.replacements.SnippetSubstitutionInvocationPlugin)1 ArrayCopyCallNode (org.graalvm.compiler.replacements.arraycopy.ArrayCopyCallNode)1