|
| 1 | +/* |
| 2 | + * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.nodes.attributes; |
| 42 | + |
| 43 | +import com.oracle.graal.python.builtins.objects.str.PString; |
| 44 | +import com.oracle.graal.python.nodes.PNodeWithContext; |
| 45 | +import com.oracle.graal.python.runtime.PythonOptions; |
| 46 | +import com.oracle.truffle.api.Assumption; |
| 47 | +import com.oracle.truffle.api.CompilerAsserts; |
| 48 | +import com.oracle.truffle.api.CompilerDirectives; |
| 49 | +import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
| 50 | +import com.oracle.truffle.api.dsl.Cached; |
| 51 | +import com.oracle.truffle.api.dsl.ImportStatic; |
| 52 | +import com.oracle.truffle.api.dsl.Specialization; |
| 53 | +import com.oracle.truffle.api.object.DynamicObject; |
| 54 | +import com.oracle.truffle.api.object.FinalLocationException; |
| 55 | +import com.oracle.truffle.api.object.IncompatibleLocationException; |
| 56 | +import com.oracle.truffle.api.object.Location; |
| 57 | +import com.oracle.truffle.api.object.Property; |
| 58 | +import com.oracle.truffle.api.object.Shape; |
| 59 | + |
| 60 | +@ImportStatic(PythonOptions.class) |
| 61 | +public abstract class WriteAttributeToDynamicObjectNode extends PNodeWithContext { |
| 62 | + |
| 63 | + public abstract boolean execute(Object primary, Object key, Object value); |
| 64 | + |
| 65 | + public abstract boolean execute(Object primary, String key, Object value); |
| 66 | + |
| 67 | + public static WriteAttributeToDynamicObjectNode create() { |
| 68 | + return WriteAttributeToDynamicObjectNodeGen.create(); |
| 69 | + } |
| 70 | + |
| 71 | + protected Location getLocationOrNull(Property prop) { |
| 72 | + return prop == null ? null : prop.getLocation(); |
| 73 | + } |
| 74 | + |
| 75 | + protected Object attrKey(Object key) { |
| 76 | + if (key instanceof PString) { |
| 77 | + return ((PString) key).getValue(); |
| 78 | + } else { |
| 79 | + return key; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @SuppressWarnings("unused") |
| 84 | + @Specialization(guards = { |
| 85 | + "dynamicObject.getShape() == cachedShape", |
| 86 | + "!layoutAssumption.isValid()" |
| 87 | + }) |
| 88 | + protected boolean updateShapeAndWrite(DynamicObject dynamicObject, Object key, Object value, |
| 89 | + @Cached("dynamicObject.getShape()") Shape cachedShape, |
| 90 | + @Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption, |
| 91 | + @Cached("create()") WriteAttributeToDynamicObjectNode nextNode) { |
| 92 | + dynamicObject.updateShape(); |
| 93 | + return nextNode.execute(dynamicObject, key, value); |
| 94 | + } |
| 95 | + |
| 96 | + protected static boolean compareKey(Object cachedKey, Object key) { |
| 97 | + return cachedKey == key; |
| 98 | + } |
| 99 | + |
| 100 | + @SuppressWarnings("unused") |
| 101 | + @Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", // |
| 102 | + guards = { |
| 103 | + "dynamicObject.getShape() == cachedShape", |
| 104 | + "compareKey(cachedKey, key)", |
| 105 | + "loc != null", |
| 106 | + "loc.canSet(value)" |
| 107 | + }, // |
| 108 | + assumptions = { |
| 109 | + "layoutAssumption" |
| 110 | + |
| 111 | + }) |
| 112 | + protected boolean doDirect(DynamicObject dynamicObject, Object key, Object value, |
| 113 | + @Cached("key") Object cachedKey, |
| 114 | + @Cached("attrKey(cachedKey)") Object attrKey, |
| 115 | + @Cached("dynamicObject.getShape()") Shape cachedShape, |
| 116 | + @Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption, |
| 117 | + @Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc) { |
| 118 | + try { |
| 119 | + loc.set(dynamicObject, value); |
| 120 | + } catch (IncompatibleLocationException | FinalLocationException e) { |
| 121 | + CompilerDirectives.transferToInterpreter(); |
| 122 | + // cannot happen due to guard |
| 123 | + throw new RuntimeException("Location.canSet is inconsistent with Location.set"); |
| 124 | + } |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + @SuppressWarnings("unused") |
| 129 | + @Specialization(limit = "getIntOption(getContext(), AttributeAccessInlineCacheMaxDepth)", // |
| 130 | + guards = { |
| 131 | + "dynamicObject.getShape() == cachedShape", |
| 132 | + "compareKey(cachedKey, key)", |
| 133 | + "loc == null || !loc.canSet(value)", |
| 134 | + "newLoc.canSet(value)" |
| 135 | + }, // |
| 136 | + assumptions = { |
| 137 | + "layoutAssumption", |
| 138 | + "newLayoutAssumption" |
| 139 | + }) |
| 140 | + protected boolean defineDirect(DynamicObject dynamicObject, Object key, Object value, |
| 141 | + @Cached("key") Object cachedKey, |
| 142 | + @Cached("attrKey(key)") Object attrKey, |
| 143 | + @Cached("dynamicObject.getShape()") Shape cachedShape, |
| 144 | + @Cached("cachedShape.getValidAssumption()") Assumption layoutAssumption, |
| 145 | + @Cached("getLocationOrNull(cachedShape.getProperty(attrKey))") Location loc, |
| 146 | + @Cached("cachedShape.defineProperty(attrKey, value, 0)") Shape newShape, |
| 147 | + @Cached("newShape.getValidAssumption()") Assumption newLayoutAssumption, |
| 148 | + @Cached("getLocationOrNull(newShape.getProperty(attrKey))") Location newLoc) { |
| 149 | + try { |
| 150 | + newLoc.set(dynamicObject, value, cachedShape, newShape); |
| 151 | + } catch (IncompatibleLocationException e) { |
| 152 | + CompilerDirectives.transferToInterpreter(); |
| 153 | + // cannot happen due to guard |
| 154 | + throw new RuntimeException("Location.canSet is inconsistent with Location.set"); |
| 155 | + } |
| 156 | + return true; |
| 157 | + } |
| 158 | + |
| 159 | + @TruffleBoundary |
| 160 | + @Specialization(guards = { |
| 161 | + "dynamicObject.getShape().isValid()" |
| 162 | + }, replaces = {"doDirect", "defineDirect"}) |
| 163 | + protected boolean doIndirect(DynamicObject dynamicObject, Object key, Object value) { |
| 164 | + Object attrKey = attrKey(key); |
| 165 | + CompilerAsserts.neverPartOfCompilation(); |
| 166 | + dynamicObject.define(attrKey, value); |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + @Specialization(guards = "!dynamicObject.getShape().isValid()") |
| 171 | + protected boolean defineDirect(DynamicObject dynamicObject, Object key, Object value) { |
| 172 | + CompilerDirectives.transferToInterpreter(); |
| 173 | + dynamicObject.updateShape(); |
| 174 | + return doIndirect(dynamicObject, key, value); |
| 175 | + } |
| 176 | +} |
0 commit comments