60
60
import com .oracle .graal .python .builtins .PythonBuiltins ;
61
61
import com .oracle .graal .python .builtins .modules .TruffleCextBuiltinsFactory .CheckFunctionResultNodeGen ;
62
62
import com .oracle .graal .python .builtins .modules .TruffleCextBuiltinsFactory .GetByteArrayNodeGen ;
63
+ import com .oracle .graal .python .builtins .modules .TruffleCextBuiltinsFactory .MakeMayRaiseWrapperNodeFactory .MayRaiseBinaryNodeGen ;
64
+ import com .oracle .graal .python .builtins .modules .TruffleCextBuiltinsFactory .MakeMayRaiseWrapperNodeFactory .MayRaiseTernaryNodeGen ;
65
+ import com .oracle .graal .python .builtins .modules .TruffleCextBuiltinsFactory .MakeMayRaiseWrapperNodeFactory .MayRaiseUnaryNodeGen ;
63
66
import com .oracle .graal .python .builtins .objects .PNone ;
64
67
import com .oracle .graal .python .builtins .objects .PythonAbstractObject ;
65
68
import com .oracle .graal .python .builtins .objects .bytes .BytesBuiltins ;
109
112
import com .oracle .graal .python .nodes .SpecialAttributeNames ;
110
113
import com .oracle .graal .python .nodes .SpecialMethodNames ;
111
114
import com .oracle .graal .python .nodes .argument .CreateArgumentsNode ;
115
+ import com .oracle .graal .python .nodes .argument .ReadArgumentNode ;
112
116
import com .oracle .graal .python .nodes .argument .ReadIndexedArgumentNode ;
113
117
import com .oracle .graal .python .nodes .argument .ReadVarArgsNode ;
114
118
import com .oracle .graal .python .nodes .argument .ReadVarKeywordsNode ;
117
121
import com .oracle .graal .python .nodes .call .InvokeNode ;
118
122
import com .oracle .graal .python .nodes .call .PythonCallNode ;
119
123
import com .oracle .graal .python .nodes .expression .BinaryComparisonNode ;
120
- import com .oracle .graal .python .nodes .function .FunctionRootNode ;
124
+ import com .oracle .graal .python .nodes .function .BuiltinFunctionRootNode ;
121
125
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
122
126
import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
127
+ import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
128
+ import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
123
129
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
124
130
import com .oracle .graal .python .nodes .interop .PForeignToPTypeNode ;
125
131
import com .oracle .graal .python .nodes .object .GetClassNode ;
154
160
import com .oracle .truffle .api .interop .UnsupportedTypeException ;
155
161
import com .oracle .truffle .api .nodes .DirectCallNode ;
156
162
import com .oracle .truffle .api .nodes .Node ;
163
+ import com .oracle .truffle .api .nodes .NodeUtil ;
157
164
import com .oracle .truffle .api .nodes .NodeVisitor ;
158
165
import com .oracle .truffle .api .nodes .RootNode ;
159
166
import com .oracle .truffle .api .profiles .BranchProfile ;
@@ -1768,46 +1775,144 @@ Object upcall(VirtualFrame frame, PythonModule cextModule, String name, Object[]
1768
1775
@ Builtin (name = "make_may_raise_wrapper" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 )
1769
1776
@ GenerateNodeFactory
1770
1777
abstract static class MakeMayRaiseWrapperNode extends PythonBuiltinNode {
1771
- static class MayRaiseWrapper extends RootNode {
1778
+ static class MayRaiseNodeFactory <T extends PythonBuiltinBaseNode > implements NodeFactory <T > {
1779
+ private final T node ;
1780
+
1781
+ public MayRaiseNodeFactory (T node ) {
1782
+ this .node = node ;
1783
+ }
1784
+
1785
+ public T createNode (Object ... arguments ) {
1786
+ return NodeUtil .cloneNode (node );
1787
+ }
1788
+
1789
+ @ SuppressWarnings ("unchecked" )
1790
+ public Class <T > getNodeClass () {
1791
+ return (Class <T >) node .getClass ();
1792
+ }
1793
+
1794
+ public List <List <Class <?>>> getNodeSignatures () {
1795
+ return null ;
1796
+ }
1797
+
1798
+ public List <Class <? extends Node >> getExecutionSignature () {
1799
+ return null ;
1800
+ }
1801
+ }
1802
+
1803
+ @ Builtin (fixedNumOfPositionalArgs = 1 )
1804
+ static abstract class MayRaiseUnaryNode extends PythonUnaryBuiltinNode {
1805
+ @ Child private CreateArgumentsNode createArgsNode ;
1806
+ @ Child private InvokeNode invokeNode ;
1807
+ private final Object errorResult ;
1808
+
1809
+ public MayRaiseUnaryNode (PFunction func , Object errorResult ) {
1810
+ this .createArgsNode = CreateArgumentsNode .create ();
1811
+ this .invokeNode = InvokeNode .create (func );
1812
+ this .errorResult = errorResult ;
1813
+ }
1814
+
1815
+ @ Specialization
1816
+ Object doit (Object argument ) {
1817
+ try {
1818
+ Object [] arguments = createArgsNode .execute (argument );
1819
+ return invokeNode .execute (null , arguments , new PKeyword [0 ]);
1820
+ } catch (PException e ) {
1821
+ getContext ().setCurrentException (e );
1822
+ return errorResult ;
1823
+ }
1824
+ }
1825
+ }
1826
+
1827
+ @ Builtin (fixedNumOfPositionalArgs = 2 )
1828
+ static abstract class MayRaiseBinaryNode extends PythonBinaryBuiltinNode {
1829
+ @ Child private CreateArgumentsNode createArgsNode ;
1830
+ @ Child private InvokeNode invokeNode ;
1831
+ private final Object errorResult ;
1832
+
1833
+ public MayRaiseBinaryNode (PFunction func , Object errorResult ) {
1834
+ this .createArgsNode = CreateArgumentsNode .create ();
1835
+ this .invokeNode = InvokeNode .create (func );
1836
+ this .errorResult = errorResult ;
1837
+ }
1838
+
1839
+ @ Specialization
1840
+ Object doit (Object arg1 , Object arg2 ) {
1841
+ try {
1842
+ Object [] arguments = createArgsNode .execute (arg1 , arg2 );
1843
+ return invokeNode .execute (null , arguments , new PKeyword [0 ]);
1844
+ } catch (PException e ) {
1845
+ getContext ().setCurrentException (e );
1846
+ return errorResult ;
1847
+ }
1848
+ }
1849
+ }
1850
+
1851
+ @ Builtin (fixedNumOfPositionalArgs = 3 )
1852
+ static abstract class MayRaiseTernaryNode extends PythonTernaryBuiltinNode {
1853
+ @ Child private CreateArgumentsNode createArgsNode ;
1854
+ @ Child private InvokeNode invokeNode ;
1855
+ private final Object errorResult ;
1856
+
1857
+ public MayRaiseTernaryNode (PFunction func , Object errorResult ) {
1858
+ this .createArgsNode = CreateArgumentsNode .create ();
1859
+ this .invokeNode = InvokeNode .create (func );
1860
+ this .errorResult = errorResult ;
1861
+ }
1862
+
1863
+ @ Specialization
1864
+ Object doit (Object arg1 , Object arg2 , Object arg3 ) {
1865
+ try {
1866
+ Object [] arguments = createArgsNode .execute (arg1 , arg2 , arg3 );
1867
+ return invokeNode .execute (null , arguments , new PKeyword [0 ]);
1868
+ } catch (PException e ) {
1869
+ getContext ().setCurrentException (e );
1870
+ return errorResult ;
1871
+ }
1872
+ }
1873
+ }
1874
+
1875
+ @ Builtin (takesVarArgs = true )
1876
+ static class MayRaiseNode extends PythonBuiltinNode {
1772
1877
@ Child private InvokeNode invokeNode ;
1773
1878
@ Child private ReadVarArgsNode readVarargsNode ;
1774
1879
@ Child private CreateArgumentsNode createArgsNode ;
1775
1880
@ Child private PythonObjectFactory factory ;
1776
1881
private final Object errorResult ;
1777
1882
1778
- @ TruffleBoundary
1779
- protected MayRaiseWrapper (PythonLanguage language , PythonObjectFactory factory , PythonCallable callable , Object errorResult ) {
1780
- super (language );
1781
- this .factory = factory ;
1883
+ protected MayRaiseNode (PythonCallable callable , Object errorResult ) {
1782
1884
this .readVarargsNode = ReadVarArgsNode .create (0 , true );
1783
1885
this .createArgsNode = CreateArgumentsNode .create ();
1784
1886
this .invokeNode = InvokeNode .create (callable );
1785
1887
this .errorResult = errorResult ;
1786
1888
}
1787
1889
1788
1890
@ Override
1789
- public boolean isCloningAllowed () {
1790
- return true ;
1791
- }
1792
-
1793
- @ Override
1794
- public Object execute (VirtualFrame frame ) {
1891
+ public final Object execute (VirtualFrame frame ) {
1795
1892
Object [] args = readVarargsNode .executeObjectArray (frame );
1796
1893
try {
1797
1894
Object [] arguments = createArgsNode .execute (args );
1798
1895
return invokeNode .execute (null , arguments , new PKeyword [0 ]);
1799
1896
} catch (PException e ) {
1800
- PythonContext context = factory .getCore ().getContext ();
1801
- context .setCurrentException (e );
1897
+ getContext ().setCurrentException (e );
1802
1898
return errorResult ;
1803
1899
}
1804
1900
}
1901
+
1902
+ @ Override
1903
+ protected ReadArgumentNode [] getArguments () {
1904
+ throw new IllegalAccessError ();
1905
+ }
1805
1906
}
1806
1907
1908
+ private static final Builtin unaryBuiltin = MayRaiseUnaryNode .class .getAnnotation (Builtin .class );
1909
+ private static final Builtin binaryBuiltin = MayRaiseBinaryNode .class .getAnnotation (Builtin .class );
1910
+ private static final Builtin ternaryBuiltin = MayRaiseTernaryNode .class .getAnnotation (Builtin .class );
1911
+ private static final Builtin varargsBuiltin = MayRaiseNode .class .getAnnotation (Builtin .class );
1912
+
1807
1913
@ Specialization
1808
1914
Object make (PFunction func , Object errorResult ) {
1809
1915
CompilerDirectives .transferToInterpreter ();
1810
- FunctionRootNode functionRootNode = (FunctionRootNode ) func .getFunctionRootNode ();
1811
1916
func .getFunctionRootNode ().accept (new NodeVisitor () {
1812
1917
public boolean visit (Node node ) {
1813
1918
if (node instanceof PythonCallNode ) {
@@ -1816,8 +1921,37 @@ public boolean visit(Node node) {
1816
1921
return true ;
1817
1922
}
1818
1923
});
1819
- return factory ().createBuiltinFunction (func .getName (), null , func .getArity (),
1820
- Truffle .getRuntime ().createCallTarget (new MayRaiseWrapper (getRootNode ().getLanguage (PythonLanguage .class ), factory (), func , errorResult )));
1924
+
1925
+ RootNode rootNode = null ;
1926
+ Arity arity = func .getArity ();
1927
+ if (arity .takesFixedNumOfPositionalArgs ()) {
1928
+ switch (arity .getMinNumOfArgs ()) {
1929
+ case 1 :
1930
+ rootNode = new BuiltinFunctionRootNode (getRootNode ().getLanguage (PythonLanguage .class ), unaryBuiltin ,
1931
+ new MayRaiseNodeFactory <PythonUnaryBuiltinNode >(MayRaiseUnaryNodeGen .create (func , errorResult )),
1932
+ true );
1933
+ break ;
1934
+ case 2 :
1935
+ rootNode = new BuiltinFunctionRootNode (getRootNode ().getLanguage (PythonLanguage .class ), binaryBuiltin ,
1936
+ new MayRaiseNodeFactory <PythonBinaryBuiltinNode >(MayRaiseBinaryNodeGen .create (func , errorResult )),
1937
+ true );
1938
+ break ;
1939
+ case 3 :
1940
+ rootNode = new BuiltinFunctionRootNode (getRootNode ().getLanguage (PythonLanguage .class ), ternaryBuiltin ,
1941
+ new MayRaiseNodeFactory <PythonTernaryBuiltinNode >(MayRaiseTernaryNodeGen .create (func , errorResult )),
1942
+ true );
1943
+ break ;
1944
+ default :
1945
+ break ;
1946
+ }
1947
+ }
1948
+ if (rootNode == null ) {
1949
+ rootNode = new BuiltinFunctionRootNode (getRootNode ().getLanguage (PythonLanguage .class ), varargsBuiltin ,
1950
+ new MayRaiseNodeFactory <PythonBuiltinNode >(new MayRaiseNode (func , errorResult )),
1951
+ true );
1952
+ }
1953
+
1954
+ return factory ().createBuiltinFunction (func .getName (), null , arity , Truffle .getRuntime ().createCallTarget (rootNode ));
1821
1955
}
1822
1956
}
1823
1957
0 commit comments