Skip to content

Commit 66a51ee

Browse files
committed
created KeywordsNode to handle CollectionBits.KWORDS
1 parent 688f18d commit 66a51ee

File tree

2 files changed

+93
-18
lines changed

2 files changed

+93
-18
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2022, 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.bytecode;
42+
43+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
44+
import com.oracle.graal.python.builtins.objects.function.PKeyword;
45+
import com.oracle.graal.python.lib.PyObjectFunctionStr;
46+
import com.oracle.graal.python.nodes.ErrorMessages;
47+
import com.oracle.graal.python.nodes.PNodeWithContext;
48+
import com.oracle.graal.python.nodes.PRaiseNode;
49+
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
50+
import com.oracle.graal.python.nodes.argument.keywords.NonMappingException;
51+
import com.oracle.truffle.api.dsl.Cached;
52+
import com.oracle.truffle.api.dsl.GenerateUncached;
53+
import com.oracle.truffle.api.dsl.Specialization;
54+
import com.oracle.truffle.api.frame.Frame;
55+
import com.oracle.truffle.api.frame.VirtualFrame;
56+
57+
@GenerateUncached
58+
public abstract class KeywordsNode extends PNodeWithContext {
59+
public abstract PKeyword[] execute(Frame virtualFrame, Object sourceCollection, int stackTop, Frame localFrame);
60+
61+
@Specialization
62+
static PKeyword[] kwords(VirtualFrame virtualFrame, Object sourceCollection, int stackTop, Frame localFrame,
63+
@Cached ExpandKeywordStarargsNode expandKeywordStarargsNode,
64+
@Cached PRaiseNode raise) {
65+
try {
66+
return expandKeywordStarargsNode.execute(sourceCollection);
67+
} catch (NonMappingException e) {
68+
Object functionName = getFunctionName(virtualFrame, stackTop, localFrame);
69+
throw raise.raise(PythonBuiltinClassType.TypeError, ErrorMessages.ARG_AFTER_MUST_BE_MAPPING, functionName, e.getObject());
70+
}
71+
}
72+
73+
private static Object getFunctionName(VirtualFrame virtualFrame, int stackTop, Frame localFrame) {
74+
/*
75+
* The instruction is only emitted when generating CALL_FUNCTION_KW. The stack layout at
76+
* this point is [kwargs kw, callable].
77+
*/
78+
Object callable = localFrame.getObject(stackTop - 2);
79+
return PyObjectFunctionStr.getUncached().execute(virtualFrame, callable);
80+
}
81+
82+
public static KeywordsNode create() {
83+
return KeywordsNodeGen.create();
84+
}
85+
86+
public static KeywordsNode getUncached() {
87+
return KeywordsNodeGen.getUncached();
88+
}
89+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
import com.oracle.graal.python.lib.PyIterNextNode;
8787
import com.oracle.graal.python.lib.PyObjectAsciiNode;
8888
import com.oracle.graal.python.lib.PyObjectDelItem;
89-
import com.oracle.graal.python.lib.PyObjectFunctionStr;
9089
import com.oracle.graal.python.lib.PyObjectGetAttr;
9190
import com.oracle.graal.python.lib.PyObjectGetIter;
9291
import com.oracle.graal.python.lib.PyObjectGetMethod;
@@ -100,7 +99,6 @@
10099
import com.oracle.graal.python.nodes.PRaiseNode;
101100
import com.oracle.graal.python.nodes.PRootNode;
102101
import com.oracle.graal.python.nodes.argument.keywords.ExpandKeywordStarargsNode;
103-
import com.oracle.graal.python.nodes.argument.keywords.NonMappingException;
104102
import com.oracle.graal.python.nodes.argument.positional.ExecutePositionalStarargsNode;
105103
import com.oracle.graal.python.nodes.builtins.ListNodes;
106104
import com.oracle.graal.python.nodes.builtins.TupleNodes;
@@ -223,6 +221,8 @@ public final class PBytecodeRootNode extends PRootNode implements BytecodeOSRNod
223221
private static final NodeSupplier<ExecutePositionalStarargsNode> NODE_EXECUTE_STARARGS = ExecutePositionalStarargsNode::create;
224222
private static final ExpandKeywordStarargsNode UNCACHED_EXPAND_KEYWORD_STARARGS = ExpandKeywordStarargsNode.getUncached();
225223
private static final NodeSupplier<ExpandKeywordStarargsNode> NODE_EXPAND_KEYWORD_STARARGS = ExpandKeywordStarargsNode::create;
224+
private static final KeywordsNode UNCACHED_KEYWORDS = KeywordsNode.getUncached();
225+
private static final NodeSupplier<KeywordsNode> NODE_KEYWORDS = KeywordsNode::create;
226226
private static final SliceNodes.CreateSliceNode UNCACHED_CREATE_SLICE = SliceNodes.CreateSliceNode.getUncached();
227227
private static final NodeSupplier<SliceNodes.CreateSliceNode> NODE_CREATE_SLICE = SliceNodes.CreateSliceNode::create;
228228
private static final ListNodes.ConstructListNode UNCACHED_CONSTRUCT_LIST = ListNodes.ConstructListNode.getUncached();
@@ -2071,13 +2071,8 @@ private void bytecodeCollectionFromCollection(VirtualFrame virtualFrame, Frame l
20712071
break;
20722072
}
20732073
case CollectionBits.KWORDS: {
2074-
try {
2075-
ExpandKeywordStarargsNode expandKeywordStarargsNode = insertChildNode(localNodes, nodeIndex, UNCACHED_EXPAND_KEYWORD_STARARGS, NODE_EXPAND_KEYWORD_STARARGS);
2076-
result = expandKeywordStarargsNode.execute(sourceCollection);
2077-
} catch (NonMappingException e) {
2078-
Object functionName = getFunctionName(virtualFrame, stackTop, localFrame);
2079-
throw PRaiseNode.getUncached().raise(PythonBuiltinClassType.TypeError, ErrorMessages.ARG_AFTER_MUST_BE_MAPPING, functionName, e.getObject());
2080-
}
2074+
KeywordsNode keywordsNode = insertChildNode(localNodes, nodeIndex, UNCACHED_KEYWORDS, NODE_KEYWORDS);
2075+
result = keywordsNode.execute(virtualFrame, sourceCollection, stackTop, localFrame);
20812076
break;
20822077
}
20832078
default:
@@ -2086,15 +2081,6 @@ private void bytecodeCollectionFromCollection(VirtualFrame virtualFrame, Frame l
20862081
stackFrame.setObject(stackTop, result);
20872082
}
20882083

2089-
private static Object getFunctionName(VirtualFrame virtualFrame, int stackTop, Frame localFrame) {
2090-
/*
2091-
* The instruction is only emitted when generating CALL_FUNCTION_KW. The stack layout at
2092-
* this point is [kwargs kw, callable].
2093-
*/
2094-
Object callable = localFrame.getObject(stackTop - 2);
2095-
return PyObjectFunctionStr.getUncached().execute(virtualFrame, callable);
2096-
}
2097-
20982084
private int bytecodeCollectionAddCollection(VirtualFrame virtualFrame, Frame stackFrame, int type, int initialStackTop, Node[] localNodes, int nodeIndex) {
20992085
int stackTop = initialStackTop;
21002086
Object collection1 = stackFrame.getObject(stackTop - 1);

0 commit comments

Comments
 (0)