@Specialization( guards = { "!copyAllAttributes || target != source", "containsMetadata(source, attrSourceProfiles)" }) public RAbstractVector copySameLength( RAbstractVector target, RAbstractVector source, // @Cached("create()") CopyOfRegAttributesNode copyOfReg, // @Cached("createDim()") RemoveAttributeNode removeDim, // @Cached("createDimNames()") RemoveAttributeNode removeDimNames, // @Cached("create()") InitAttributesNode initAttributes, // @Cached("createNames()") PutAttributeNode putNames, // @Cached("createDim()") PutAttributeNode putDim, // @Cached("createBinaryProfile()") ConditionProfile noDimensions, // @Cached("createBinaryProfile()") ConditionProfile hasNamesSource, // @Cached("createBinaryProfile()") ConditionProfile hasDimNames) { RVector result = target.materialize(); if (copyAllAttributes) { copyOfReg.execute(source, result); } int[] newDimensions = source.getDimensions(); if (noDimensions.profile(newDimensions == null)) { RAttributes attributes = result.getAttributes(); if (attributes != null) { removeDim.execute(attributes); removeDimNames.execute(attributes); result.setInternalDimNames(null); } result.setInternalDimensions(null); RStringVector vecNames = source.getNames(attrSourceProfiles); if (hasNamesSource.profile(vecNames != null)) { putNames.execute(initAttributes.execute(result), vecNames); result.setInternalNames(vecNames); return result; } return result; } putDim.execute( initAttributes.execute(result), RDataFactory.createIntVector(newDimensions, RDataFactory.COMPLETE_VECTOR)); result.setInternalDimensions(newDimensions); RList newDimNames = source.getDimNames(attrSourceProfiles); if (hasDimNames.profile(newDimNames != null)) { result.getAttributes().put(RRuntime.DIMNAMES_ATTR_KEY, newDimNames); newDimNames.elementNamePrefix = RRuntime.DIMNAMES_LIST_ELEMENT_NAME_PREFIX; result.setInternalDimNames(newDimNames); return result; } return result; }
@ExplodeLoop public boolean getContainsNA(PositionProfile[] profiles) { if (positionsCheck.length == 1) { return containsNAProfile.profile(profiles[0].containsNA); } else { boolean containsNA = false; for (int i = 0; i < positionsCheck.length; i++) { containsNA |= profiles[i].containsNA; } return containsNAProfile.profile(containsNA); } }
private Object tryConvertToHash( VirtualFrame frame, final int argumentCount, final Object lastArgument) { if (respondsToToHashProfile.profile(respondToToHash(frame, lastArgument))) { final Object converted = callToHash(frame, lastArgument); if (convertedIsHashProfile.profile(RubyGuards.isRubyHash(converted))) { RubyArguments.setArgument(frame, argumentCount - 1, converted); return converted; } } return null; }
@Override public void execute(VirtualFrame frame, Object value, MaterializedFrame enclosingFrame) { MaterializedFrame profiledEnclosingFrame = enclosingFrameProfile.profile(enclosingFrame); if (hasValueProfile.profile(writeNode.getFrameSlotNode().hasValue(profiledEnclosingFrame))) { writeNode.execute(frame, value, profiledEnclosingFrame); } else { MaterializedFrame superFrame = RArguments.getEnclosingFrame(profiledEnclosingFrame); if (nullSuperFrameProfile.profile(superFrame == null)) { // Might be the case if "{ x <<- 42 }": This is in globalEnv! superFrame = REnvironment.globalEnv().getFrame(); } nextNode.execute(frame, value, superFrame); } }
@Specialization(guards = {"!copyAllAttributes || target != source", "containsMetadata(source)"}) protected RAbstractVector copySameLength( RAbstractVector target, RAbstractVector source, // @Cached("create()") CopyOfRegAttributesNode copyOfReg, // @Cached("createDim()") RemoveFixedAttributeNode removeDim, // @Cached("createDimNames()") RemoveFixedAttributeNode removeDimNames, // @Cached("create()") InitAttributesNode initAttributes, // @Cached("createNames()") SetFixedAttributeNode putNames, // @Cached("createDim()") SetFixedAttributeNode putDim, // @Cached("createDimNames()") SetFixedAttributeNode putDimNames, // @Cached("createBinaryProfile()") ConditionProfile noDimensions, // @Cached("createBinaryProfile()") ConditionProfile hasNamesSource, // @Cached("createBinaryProfile()") ConditionProfile hasDimNames, @Cached("create()") GetDimAttributeNode getDimsNode) { RVector<?> result = target.materialize(); if (copyAllAttributes) { copyOfReg.execute(source, result); } int[] newDimensions = getDimsNode.getDimensions(source); if (noDimensions.profile(newDimensions == null)) { DynamicObject attributes = result.getAttributes(); if (attributes != null) { removeDim.execute(attributes); removeDimNames.execute(attributes); } RStringVector vecNames = getNamesNode.getNames(source); if (hasNamesSource.profile(vecNames != null)) { putNames.execute(initAttributes.execute(result), vecNames); return result; } return result; } putDim.execute( initAttributes.execute(result), RDataFactory.createIntVector(newDimensions, RDataFactory.COMPLETE_VECTOR)); RList newDimNames = getDimNamesNode.getDimNames(source); if (hasDimNames.profile(newDimNames != null)) { putDimNames.execute(result.getAttributes(), newDimNames); newDimNames.elementNamePrefix = RRuntime.DIMNAMES_LIST_ELEMENT_NAME_PREFIX; return result; } return result; }
@Override public Object execute(VirtualFrame frame) { final int argumentCount = RubyArguments.getArgumentsCount(frame); if (notEnoughArgumentsProfile.profile(argumentCount <= minArgumentCount)) { return null; } final Object lastArgument = RubyArguments.getArgument(frame, argumentCount - 1); if (lastArgumentIsHashProfile.profile(RubyGuards.isRubyHash(lastArgument))) { return lastArgument; } return tryConvertToHash(frame, argumentCount, lastArgument); }
private Object lookupRestKeywordArgumentHash(VirtualFrame frame) { final Object hash = readUserKeywordsHashNode.execute(frame); if (noHash.profile(hash == null)) { return Layouts.HASH.createHash( coreLibrary().getHashFactory(), null, 0, null, null, null, null, false); } CompilerDirectives.bailout("Ruby keyword arguments aren't optimized"); final DynamicObject hashObject = (DynamicObject) hash; final List<Map.Entry<Object, Object>> entries = new ArrayList<>(); outer: for (Map.Entry<Object, Object> keyValue : HashOperations.iterableKeyValues(hashObject)) { if (!RubyGuards.isRubySymbol(keyValue.getKey())) { continue; } for (String excludedKeyword : excludedKeywords) { if (excludedKeyword.equals(keyValue.getKey().toString())) { continue outer; } } entries.add(keyValue); } return BucketsStrategy.create( getContext(), entries, Layouts.HASH.getCompareByIdentity(hashObject)); }
@Specialization(guards = {"each > 1", "hasNames(x)"}) protected RAbstractVector repEachNames( RAbstractVector x, RAbstractIntVector times, int lengthOut, int each, @Cached("create()") InitAttributesNode initAttributes, @Cached("createNames()") SetFixedAttributeNode putNames) { if (times.getLength() > 1) { errorBranch.enter(); throw invalidTimes(); } RAbstractVector input = handleEach(x, each); RStringVector names = (RStringVector) handleEach(getNames.getNames(x), each); RVector<?> r; if (lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut))) { names = (RStringVector) handleLengthOut(names, lengthOut, false); r = handleLengthOut(input, lengthOut, false); } else { names = (RStringVector) handleTimes(names, times, false); r = handleTimes(input, times, false); } putNames.execute(initAttributes.execute(r), names); return r; }
/** Replicate the vector a given number of times. */ private RVector<?> handleTimes( RAbstractVector x, RAbstractIntVector times, boolean copyIfSameSize) { if (oneTimeGiven.profile(times.getLength() == 1)) { // only one times value is given final int howManyTimes = times.getDataAt(0); if (howManyTimes < 0) { errorBranch.enter(); throw invalidTimes(); } if (replicateOnce.profile(howManyTimes == 1)) { return (RVector<?>) (copyIfSameSize ? x.copy() : x); } else { return x.copyResized(x.getLength() * howManyTimes, false); } } else { // times is a vector with several elements if (x.getLength() != times.getLength()) { errorBranch.enter(); invalidTimes(); } // iterate once over the times vector to determine result vector size int resultLength = 0; for (int i = 0; i < times.getLength(); i++) { int t = times.getDataAt(i); if (t < 0) { errorBranch.enter(); throw invalidTimes(); } resultLength += t; } // create and populate result vector RVector<?> r = x.createEmptySameType(resultLength, x.isComplete()); int wp = 0; // write pointer for (int i = 0; i < x.getLength(); i++) { for (int j = 0; j < times.getDataAt(i); ++j, ++wp) { r.transferElementSameType(wp, x, i); } } return r; } }
@Specialization(guards = {"each <= 1", "!hasNames(x)"}) protected RAbstractVector repNoEachNoNames( RAbstractVector x, RAbstractIntVector times, int lengthOut, @SuppressWarnings("unused") int each) { if (lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut))) { return handleLengthOut(x, lengthOut, true); } else { return handleTimes(x, times, true); } }
@Specialization(guards = {"each > 1", "!hasNames(x)"}) protected RAbstractVector repEachNoNames( RAbstractVector x, RAbstractIntVector times, int lengthOut, int each) { if (times.getLength() > 1) { errorBranch.enter(); throw invalidTimes(); } RAbstractVector input = handleEach(x, each); if (lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut))) { return handleLengthOut(input, lengthOut, false); } else { return handleTimes(input, times, false); } }
@Specialization protected Object getDimNames(RAbstractContainer container) { controlVisibility(); RList names; if (container instanceof RDataFrame) { dataframeProfile.enter(); names = ((RDataFrame) container).getVector().getDimNames(); } else if (container instanceof RFactor) { factorProfile.enter(); names = ((RFactor) container).getVector().getDimNames(); } else { otherProfile.enter(); names = container.getDimNames(attrProfiles); } return nullProfile.profile(names == null) ? RNull.instance : names; }
@Specialization(guards = "strategy.matches(array)", limit = "ARRAY_STRATEGIES") public boolean ensureCapacity( DynamicObject array, int requiredCapacity, @Cached("of(array)") ArrayStrategy strategy, @Cached("createCountingProfile()") ConditionProfile extendProfile) { final ArrayMirror mirror = strategy.newMirror(array); if (extendProfile.profile(mirror.getLength() < requiredCapacity)) { final int capacity = ArrayUtils.capacity(getContext(), mirror.getLength(), requiredCapacity); Layouts.ARRAY.setStore(array, mirror.copyArrayAndMirror(capacity).getArray()); return true; } else { return false; } }
@Override public Object execute(VirtualFrame frame) { if (toEnd) { setSourcePosition(frame, getSourceLength(frame)); } else { final int position = getSourcePosition(frame); if (rangeProfile.profile(position + 1 > getSourceLength(frame))) { throw new OutsideOfStringException(); } setSourcePosition(frame, getSourcePosition(frame) + 1); } return null; }
@Specialization( guards = {"x.getLength() == 1", "times.getLength() == 1", "each <= 1", "!hasNames(x)"}) protected RAbstractVector repNoEachNoNamesSimple( RAbstractDoubleVector x, RAbstractIntVector times, int lengthOut, @SuppressWarnings("unused") int each) { int t = times.getDataAt(0); if (t < 0) { errorBranch.enter(); throw invalidTimes(); } int length = lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut)) ? lengthOut : t; double[] data = new double[length]; Arrays.fill(data, x.getDataAt(0)); return RDataFactory.createDoubleVector(data, !RRuntime.isNA(x.getDataAt(0))); }
@Specialization(guards = {"each <= 1", "hasNames(x)"}) protected RAbstractVector repNoEachNames( RAbstractVector x, RAbstractIntVector times, int lengthOut, @SuppressWarnings("unused") int each, @Cached("create()") InitAttributesNode initAttributes, @Cached("createNames()") SetFixedAttributeNode putNames) { RStringVector names; RVector<?> r; if (lengthOutOrTimes.profile(!RRuntime.isNA(lengthOut))) { names = (RStringVector) handleLengthOut(getNames.getNames(x), lengthOut, true); r = handleLengthOut(x, lengthOut, true); } else { names = (RStringVector) handleTimes(getNames.getNames(x), times, true); r = handleTimes(x, times, true); } putNames.execute(initAttributes.execute(r), names); return r; }
private void checkSlotAssign(VirtualFrame frame, Object object, String name, Object value) { // TODO: optimize using a mechanism similar to overrides? if (checkSlotAssignFunction == null) { CompilerDirectives.transferToInterpreterAndInvalidate(); checkSlotAssignFunction = (RFunction) checkAtAssignmentFind.execute(frame); checkAtAssignmentCall = insert(CallRFunctionNode.create(checkSlotAssignFunction.getTarget())); assert objClassHierarchy == null && valClassHierarchy == null; objClassHierarchy = insert(ClassHierarchyNodeGen.create(true, false)); valClassHierarchy = insert(ClassHierarchyNodeGen.create(true, false)); } RStringVector objClass = objClassHierarchy.execute(object); RStringVector valClass = objClassHierarchy.execute(value); RFunction currentFunction = (RFunction) checkAtAssignmentFind.execute(frame); if (cached.profile(currentFunction == checkSlotAssignFunction)) { // TODO: technically, someone could override checkAtAssignment function and access the // caller, but it's rather unlikely checkAtAssignmentCall.execute( frame, checkSlotAssignFunction, RCaller.create(frame, getOriginalCall()), null, new Object[] {objClass, name, valClass}, SIGNATURE, checkSlotAssignFunction.getEnclosingFrame(), null); } else { // slow path RContext.getEngine() .evalFunction( currentFunction, frame.materialize(), RCaller.create(frame, getOriginalCall()), null, objClass, name, valClass); } }
@Specialization(guards = "isRubyModule(lexicalParentModule)") public Object defineModule(VirtualFrame frame, DynamicObject lexicalParentModule) { final RubyConstant constant = lookupForExistingModule(frame, name, lexicalParentModule); final DynamicObject definingModule; if (needToDefineProfile.profile(constant == null)) { definingModule = ModuleNodes.createModule( getContext(), coreLibrary().getModuleClass(), lexicalParentModule, name, this); } else { final Object constantValue = constant.getValue(); if (!RubyGuards.isRubyModule(constantValue) || RubyGuards.isRubyClass(constantValue)) { errorProfile.enter(); throw new RaiseException(coreExceptions().typeErrorIsNotA(name, "module", this)); } definingModule = (DynamicObject) constantValue; } return definingModule; }