Skip to content

Commit bf87b29

Browse files
committed
Introduce base classes for nodes with indirect calls
1 parent 59e612c commit bf87b29

File tree

5 files changed

+169
-21
lines changed

5 files changed

+169
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketUtils.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ public interface SocketFunction<T> {
6868
* with timeouts and retrying the call on EINTR. Must be called with GIL held.
6969
*/
7070
public static <T> T callSocketFunctionWithRetry(PNodeWithRaise node, PosixSupportLibrary posixLib, Object posixSupport, GilNode gil, PSocket socket, SocketFunction<T> function,
71-
boolean writing, boolean connect)
72-
throws PosixException {
71+
boolean writing, boolean connect) throws PosixException {
7372
return callSocketFunctionWithRetry(node, posixLib, posixSupport, gil, socket, function, writing, connect, null);
7473
}
7574

@@ -78,8 +77,7 @@ public static <T> T callSocketFunctionWithRetry(PNodeWithRaise node, PosixSuppor
7877
* connections with timeouts and retrying the call on EINTR. Must be called with GIL held.
7978
*/
8079
public static <T> T callSocketFunctionWithRetry(PNodeWithRaise node, PosixSupportLibrary posixLib, Object posixSupport, GilNode gil, PSocket socket, SocketFunction<T> function,
81-
boolean writing, boolean connect, TimeoutHelper timeoutHelperIn)
82-
throws PosixException {
80+
boolean writing, boolean connect, TimeoutHelper timeoutHelperIn) throws PosixException {
8381
TimeoutHelper timeoutHelper = timeoutHelperIn;
8482
if (timeoutHelper == null && socket.getTimeoutNs() > 0) {
8583
timeoutHelper = new TimeoutHelper(socket.getTimeoutNs());
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, 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;
42+
43+
import com.oracle.truffle.api.Assumption;
44+
import com.oracle.truffle.api.CompilerDirectives;
45+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
46+
import com.oracle.truffle.api.Truffle;
47+
48+
public class PNodeWithIndirectCall extends PNodeWithContext implements IndirectCallNode {
49+
50+
@CompilationFinal private Assumption nativeCodeDoesntNeedExceptionState;
51+
@CompilationFinal private Assumption nativeCodeDoesntNeedMyFrame;
52+
53+
@Override
54+
public final Assumption needNotPassFrameAssumption() {
55+
if (nativeCodeDoesntNeedMyFrame == null) {
56+
CompilerDirectives.transferToInterpreterAndInvalidate();
57+
nativeCodeDoesntNeedMyFrame = Truffle.getRuntime().createAssumption();
58+
}
59+
return nativeCodeDoesntNeedMyFrame;
60+
}
61+
62+
@Override
63+
public final Assumption needNotPassExceptionAssumption() {
64+
if (nativeCodeDoesntNeedExceptionState == null) {
65+
CompilerDirectives.transferToInterpreterAndInvalidate();
66+
nativeCodeDoesntNeedExceptionState = Truffle.getRuntime().createAssumption();
67+
}
68+
return nativeCodeDoesntNeedExceptionState;
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2021, 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;
42+
43+
import com.oracle.truffle.api.Assumption;
44+
import com.oracle.truffle.api.CompilerDirectives;
45+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
46+
import com.oracle.truffle.api.Truffle;
47+
48+
public class PNodeWithRaiseAndIndirectCall extends PNodeWithRaise implements IndirectCallNode {
49+
50+
@CompilationFinal private Assumption nativeCodeDoesntNeedExceptionState;
51+
@CompilationFinal private Assumption nativeCodeDoesntNeedMyFrame;
52+
53+
@Override
54+
public final Assumption needNotPassFrameAssumption() {
55+
if (nativeCodeDoesntNeedMyFrame == null) {
56+
CompilerDirectives.transferToInterpreterAndInvalidate();
57+
nativeCodeDoesntNeedMyFrame = Truffle.getRuntime().createAssumption();
58+
}
59+
return nativeCodeDoesntNeedMyFrame;
60+
}
61+
62+
@Override
63+
public final Assumption needNotPassExceptionAssumption() {
64+
if (nativeCodeDoesntNeedExceptionState == null) {
65+
CompilerDirectives.transferToInterpreterAndInvalidate();
66+
nativeCodeDoesntNeedExceptionState = Truffle.getRuntime().createAssumption();
67+
}
68+
return nativeCodeDoesntNeedExceptionState;
69+
}
70+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/PythonBuiltinBaseNode.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,18 @@
4343
import com.oracle.graal.python.builtins.Python3Core;
4444
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4545
import com.oracle.graal.python.builtins.objects.exception.OSErrorEnum;
46-
import com.oracle.graal.python.nodes.IndirectCallNode;
4746
import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
48-
import com.oracle.graal.python.nodes.PNodeWithRaise;
47+
import com.oracle.graal.python.nodes.PNodeWithRaiseAndIndirectCall;
4948
import com.oracle.graal.python.runtime.PosixSupportLibrary.PosixException;
5049
import com.oracle.graal.python.runtime.exception.PException;
5150
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
52-
import com.oracle.truffle.api.Assumption;
5351
import com.oracle.truffle.api.CompilerDirectives;
54-
import com.oracle.truffle.api.Truffle;
5552
import com.oracle.truffle.api.frame.VirtualFrame;
5653
import com.oracle.truffle.api.profiles.ConditionProfile;
5754

58-
public abstract class PythonBuiltinBaseNode extends PNodeWithRaise implements IndirectCallNode {
55+
public abstract class PythonBuiltinBaseNode extends PNodeWithRaiseAndIndirectCall {
5956
@Child private PythonObjectFactory objectFactory;
6057
@Child private PConstructAndRaiseNode constructAndRaiseNode;
61-
private final Assumption dontNeedExceptionState = Truffle.getRuntime().createAssumption();
62-
private final Assumption dontNeedCallerFrame = Truffle.getRuntime().createAssumption();
63-
64-
@Override
65-
public Assumption needNotPassFrameAssumption() {
66-
return dontNeedCallerFrame;
67-
}
68-
69-
@Override
70-
public Assumption needNotPassExceptionAssumption() {
71-
return dontNeedExceptionState;
72-
}
7358

7459
protected final PythonObjectFactory factory() {
7560
if (objectFactory == null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/ExecutionContext.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
4747
import com.oracle.graal.python.builtins.objects.function.PArguments;
4848
import com.oracle.graal.python.nodes.IndirectCallNode;
49+
import com.oracle.graal.python.nodes.PNodeWithRaiseAndIndirectCall;
4950
import com.oracle.graal.python.nodes.PRootNode;
5051
import com.oracle.graal.python.nodes.control.TopLevelExceptionHandler;
5152
import com.oracle.graal.python.nodes.frame.MaterializeFrameNode;
@@ -354,6 +355,20 @@ public static Object enter(VirtualFrame frame, PythonLanguage language, PythonCo
354355
return enter(frame, pythonThreadState, needsCallerFrame, needsExceptionState, callNode);
355356
}
356357

358+
public static Object enter(VirtualFrame frame, PNodeWithRaiseAndIndirectCall indirectCallNode) {
359+
if (frame == null || indirectCallNode == null) {
360+
return null;
361+
}
362+
boolean needsCallerFrame = indirectCallNode.calleeNeedsCallerFrame();
363+
boolean needsExceptionState = indirectCallNode.calleeNeedsExceptionState();
364+
if (!needsCallerFrame && !needsExceptionState) {
365+
return null;
366+
}
367+
368+
PythonThreadState pythonThreadState = indirectCallNode.getContext().getThreadState(indirectCallNode.getLanguage());
369+
return enter(frame, pythonThreadState, needsCallerFrame, needsExceptionState, indirectCallNode);
370+
}
371+
357372
/**
358373
* @see #enter(VirtualFrame, PythonLanguage, PythonContext, IndirectCallNode)
359374
*/
@@ -397,6 +412,16 @@ public static void exit(VirtualFrame frame, PythonLanguage language, PythonConte
397412
}
398413
}
399414

415+
public static void exit(VirtualFrame frame, PNodeWithRaiseAndIndirectCall indirectCallNode, Object savedState) {
416+
if (savedState != null && frame != null) {
417+
PythonContext context = indirectCallNode.getContext();
418+
if (context != null) {
419+
PythonLanguage language = indirectCallNode.getLanguage();
420+
exit(frame, context.getThreadState(language), savedState);
421+
}
422+
}
423+
}
424+
400425
/**
401426
* @see #exit(VirtualFrame, PythonLanguage, PythonContext, Object)
402427
*/

0 commit comments

Comments
 (0)