ArrayConcatNode.java

  1. /*
  2.  * Copyright (c) 2013, 2014 Oracle and/or its affiliates. All rights reserved. This
  3.  * code is released under a tri EPL/GPL/LGPL license. You can use it,
  4.  * redistribute it and/or modify it under the terms of the:
  5.  *
  6.  * Eclipse Public License version 1.0
  7.  * GNU General Public License version 2
  8.  * GNU Lesser General Public License version 2.1
  9.  */
  10. package org.jruby.truffle.nodes.core;

  11. import com.oracle.truffle.api.frame.VirtualFrame;
  12. import com.oracle.truffle.api.nodes.ExplodeLoop;
  13. import com.oracle.truffle.api.source.SourceSection;
  14. import com.oracle.truffle.api.utilities.BranchProfile;
  15. import org.jruby.truffle.nodes.RubyNode;
  16. import org.jruby.truffle.runtime.RubyContext;
  17. import org.jruby.truffle.runtime.core.RubyArray;

  18. /**
  19.  * Concatenate arrays.
  20.  */
  21. public final class ArrayConcatNode extends RubyNode {

  22.     @Children protected final RubyNode[] children;
  23.     @Child protected ArrayBuilderNode arrayBuilderNode;

  24.     private final BranchProfile appendArrayProfile = BranchProfile.create();
  25.     private final BranchProfile appendObjectProfile = BranchProfile.create();

  26.     public ArrayConcatNode(RubyContext context, SourceSection sourceSection, RubyNode[] children) {
  27.         super(context, sourceSection);
  28.         assert children.length > 1;
  29.         this.children = children;
  30.         arrayBuilderNode = new ArrayBuilderNode.UninitializedArrayBuilderNode(context);
  31.     }

  32.     @Override
  33.     public RubyArray execute(VirtualFrame frame) {
  34.         Object store = arrayBuilderNode.start();
  35.         int length = 0;
  36.         if (children.length == 1) {
  37.             return executeSingle(frame, store, length);
  38.         } else {
  39.             return executeArray(frame, store, length);
  40.         }
  41.     }

  42.     @ExplodeLoop
  43.     private RubyArray executeSingle(VirtualFrame frame, Object store, int length) {
  44.         final Object childObject = children[0].execute(frame);
  45.         if (childObject instanceof RubyArray) {
  46.             appendArrayProfile.enter();
  47.             final RubyArray childArray = (RubyArray) childObject;
  48.             store = arrayBuilderNode.ensure(store, length + childArray.getSize());
  49.             store = arrayBuilderNode.append(store, length, childArray);
  50.             length += childArray.getSize();
  51.         } else {
  52.             appendObjectProfile.enter();
  53.             store = arrayBuilderNode.ensure(store, length + 1);
  54.             store = arrayBuilderNode.append(store, length, childObject);
  55.             length++;
  56.         }
  57.         return new RubyArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilderNode.finish(store, length), length);
  58.     }

  59.     @ExplodeLoop
  60.     private RubyArray executeArray(VirtualFrame frame, Object store, int length) {
  61.         for (int n = 0; n < children.length; n++) {
  62.             final Object childObject = children[n].execute(frame);

  63.             if (childObject instanceof RubyArray) {
  64.                 appendArrayProfile.enter();
  65.                 final RubyArray childArray = (RubyArray) childObject;
  66.                 store = arrayBuilderNode.ensure(store, length + childArray.getSize());
  67.                 store = arrayBuilderNode.append(store, length, childArray);
  68.                 length += childArray.getSize();
  69.             } else {
  70.                 appendObjectProfile.enter();
  71.                 store = arrayBuilderNode.ensure(store, length + 1);
  72.                 store = arrayBuilderNode.append(store, length, childObject);
  73.                 length++;
  74.             }
  75.         }

  76.         return new RubyArray(getContext().getCoreLibrary().getArrayClass(), arrayBuilderNode.finish(store, length), length);
  77.     }

  78.     @ExplodeLoop
  79.     @Override
  80.     public void executeVoid(VirtualFrame frame) {
  81.         for (int n = 0; n < children.length; n++) {
  82.             children[n].executeVoid(frame);
  83.         }
  84.     }

  85. }