diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java index 1c6562db6396bdce9c7600220ce3fcbee4ee6afd..c42616fccf5020f685de15630ff0640d6549dfb4 100644 --- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java +++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/MiscNodes.java @@ -43,7 +43,6 @@ import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctions.SetNames import com.oracle.truffle.r.nodes.attributes.SpecialAttributesFunctionsFactory.SetNamesAttributeNodeGen; import com.oracle.truffle.r.nodes.builtin.EnvironmentNodes.GetFunctionEnvironmentNode; import com.oracle.truffle.r.nodes.builtin.casts.fluent.CastNodeBuilder; -import com.oracle.truffle.r.nodes.builtin.casts.fluent.HeadPhaseBuilder; import com.oracle.truffle.r.nodes.objects.NewObject; import com.oracle.truffle.r.nodes.objects.NewObjectNodeGen; import com.oracle.truffle.r.nodes.unary.CastNode; @@ -57,8 +56,6 @@ import com.oracle.truffle.r.runtime.data.RRawVector; import com.oracle.truffle.r.runtime.data.RSymbol; import com.oracle.truffle.r.runtime.data.RTypes; import com.oracle.truffle.r.runtime.data.model.RAbstractContainer; -import com.oracle.truffle.r.runtime.data.model.RAbstractDoubleVector; -import com.oracle.truffle.r.runtime.data.nodes.GetDataAt; import com.oracle.truffle.r.runtime.env.REnvironment; import com.oracle.truffle.r.runtime.gnur.SEXPTYPE; @@ -266,26 +263,17 @@ public final class MiscNodes { @Specialization protected RRawVector octSize(Object size, @Cached("create()") SizeToOctalRawNode sizeToOctal, - @Cached("createCast()") CastNode castToDoubleNode, - @Cached("create()") GetDataAt.Double getDataNode) { - - Object val = castToDoubleNode.doCast(size); - if (val instanceof RAbstractDoubleVector) { - RAbstractDoubleVector vec = (RAbstractDoubleVector) val; - return sizeToOctal.execute(getDataNode.get(vec, vec.getInternalStore(), 0)); - } - return sizeToOctal.execute(val); + @Cached("createCast()") CastNode castToDoubleNode) { + return sizeToOctal.execute(castToDoubleNode.doCast(size)); } protected CastNode createCast() { - HeadPhaseBuilder<Double> findFirst = CastNodeBuilder.newCastBuilder().mustNotBeMissing().allowNull().asDoubleVector().findFirst(); - return findFirst.buildCastNode(); + return CastNodeBuilder.newCastBuilder().mustNotBeMissing().allowNull().asDoubleVector().findFirst().buildCastNode(); } public static OctSizeNode create() { return OctSizeNodeGen.create(); } } - } diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/SizeToOctalRawNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/SizeToOctalRawNode.java index f830844351297e068af382b916b06b6a54c60932..83ddb43115f9529903a56305eee5b6cf7e65a8b7 100644 --- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/SizeToOctalRawNode.java +++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/SizeToOctalRawNode.java @@ -1,97 +1,70 @@ /* - * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * This material is distributed under the GNU General Public License + * Version 2. You may review the terms of this license at + * http://www.gnu.org/licenses/gpl-2.0.html * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. + * Copyright (c) 2000-2013, The R Core Team + * Copyright (c) 2017, Oracle and/or its affiliates * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. + * All rights reserved. */ package com.oracle.truffle.r.nodes.unary; -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.util.Arrays; - -import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; -import com.oracle.truffle.api.dsl.Cached; +import com.oracle.truffle.api.dsl.Fallback; +import com.oracle.truffle.api.dsl.ImportStatic; import com.oracle.truffle.api.dsl.Specialization; import com.oracle.truffle.r.runtime.RError; import com.oracle.truffle.r.runtime.RRuntime; +import com.oracle.truffle.r.runtime.RType; import com.oracle.truffle.r.runtime.data.RDataFactory; -import com.oracle.truffle.r.runtime.data.RNull; +import com.oracle.truffle.r.runtime.data.RDataFactory.VectorFactory; import com.oracle.truffle.r.runtime.data.RRawVector; -import com.oracle.truffle.r.runtime.data.nodes.SetDataAt; +import com.oracle.truffle.r.runtime.data.nodes.VectorAccess; +import com.oracle.truffle.r.runtime.data.nodes.VectorAccess.RandomIterator; +@ImportStatic(RRuntime.class) public abstract class SizeToOctalRawNode extends UnaryNode { - private Charset asciiCharset; + @Child private VectorFactory factory = VectorFactory.create(); + @Child private VectorAccess resultAccess = VectorAccess.createNew(RType.Raw); public abstract RRawVector execute(Object size); - @Specialization - protected RRawVector doInt(int s) { - return RDataFactory.createRawVector(toOctalAsciiString(s)); - } - - @TruffleBoundary - private byte[] toOctalAsciiString(int s) { - if (asciiCharset == null) { - asciiCharset = Charset.forName("US-ASCII"); + @Specialization(guards = "!isNA(size)") + protected RRawVector doInt(int size) { + if (size < 0) { + throw RError.error(RError.SHOW_CALLER, RError.Message.GENERIC, "size must be finite, >= 0 and < 2^33"); } - - ByteBuffer encode = asciiCharset.encode(Integer.toOctalString(s)); - // reverse - byte[] result = new byte[11]; - Arrays.fill(result, (byte) '0'); - for (int i = result.length - 1; i >= 0 && encode.hasRemaining(); i--) { - result[i] = encode.get(); - } - return result; + return toOctal(size); } // Transcribed from ".../utils/src/stubs.c" - @Specialization - protected RRawVector doDouble(double size, - @Cached("create()") SetDataAt.Raw setDataNode) { - - double s = size; - if (!RRuntime.isFinite(s) && s >= 0) { - throw RError.error(RError.SHOW_CALLER, RError.Message.GENERIC, "size must be finite and >= 0"); + @Specialization(guards = "!isNAorNaN(size)") + protected RRawVector doDouble(double size) { + if (!RRuntime.isFinite(size) || size < 0 || size >= 8589934592d /* 2^33 */) { + throw RError.error(RError.SHOW_CALLER, RError.Message.GENERIC, "size must be finite, >= 0 and < 2^33"); } + return toOctal((long) size); + } - RRawVector ans = RDataFactory.createRawVector(11); - byte[] store = ans.getInternalStore(); - - for (int i = 0; i < 11; i++) { - double s2 = Math.floor(s / 8.0); - double t = s - 8.0 * s2; - s = s2; - setDataNode.setDataAtAsObject(ans, store, 10 - i, (byte) (48.0 + t)); + private RRawVector toOctal(long size) { + RRawVector ans = factory.createRawVector(11); + try (RandomIterator iter = resultAccess.randomAccess(ans)) { + long s = size; + for (int i = 0; i < 11; i++) { + resultAccess.setRaw(iter, 10 - i, (byte) (48.0 + (s % 8))); + s /= 8; + } } return ans; } - @Specialization - protected RRawVector doNull(@SuppressWarnings("unused") RNull n) { + @Fallback + protected RRawVector doNull(@SuppressWarnings("unused") Object obj) { return RDataFactory.createRawVector(11); } public static SizeToOctalRawNode create() { return SizeToOctalRawNodeGen.create(); - } } diff --git a/mx.fastr/copyrights/overrides b/mx.fastr/copyrights/overrides index 7676e3a4aa78314400a28ca79fc7e0cb5628b01d..768ff8b911ffa742dbf5b52e356063cc315be8b1 100644 --- a/mx.fastr/copyrights/overrides +++ b/mx.fastr/copyrights/overrides @@ -195,6 +195,7 @@ com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/objects/LoadMethod.jav com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/objects/NewObject.java,gnu_r_gentleman_ihaka.copyright com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/InheritsNode.java,purdue.copyright com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/IsFactorNode.java,purdue.copyright +com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/SizeToOctalRawNode.java,gnu_r.core.copyright com.oracle.truffle.r.parser/src/com/oracle/truffle/r/parser/R.g,purdue.copyright com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RTypedValue.java,gnu_r_gentleman_ihaka.copyright com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/DLL.java,gnu_r.copyright