64
64
import com .oracle .graal .python .builtins .objects .cext .hpy .HPyExternalFunctionNodesFactory .HPyCheckHandleResultNodeGen ;
65
65
import com .oracle .graal .python .builtins .objects .cext .hpy .HPyExternalFunctionNodesFactory .HPyCheckPrimitiveResultNodeGen ;
66
66
import com .oracle .graal .python .builtins .objects .cext .hpy .HPyExternalFunctionNodesFactory .HPyExternalFunctionInvokeNodeGen ;
67
+ import com .oracle .graal .python .builtins .objects .common .IndexNodes .NormalizeIndexNode ;
67
68
import com .oracle .graal .python .builtins .objects .exception .PBaseException ;
68
69
import com .oracle .graal .python .builtins .objects .function .PArguments ;
69
70
import com .oracle .graal .python .builtins .objects .function .PBuiltinFunction ;
70
71
import com .oracle .graal .python .builtins .objects .function .PKeyword ;
71
72
import com .oracle .graal .python .builtins .objects .function .Signature ;
73
+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
72
74
import com .oracle .graal .python .nodes .ErrorMessages ;
73
75
import com .oracle .graal .python .nodes .IndirectCallNode ;
74
76
import com .oracle .graal .python .nodes .PGuards ;
@@ -645,7 +647,7 @@ public Signature getSignature() {
645
647
}
646
648
}
647
649
648
- static final class HPyMethSSizeArgFuncRoot extends HPyMethodDescriptorRootNode {
650
+ static class HPyMethSSizeArgFuncRoot extends HPyMethodDescriptorRootNode {
649
651
private static final Signature SIGNATURE = new Signature (2 , false , -1 , false , new String []{"$self" , "n" }, KEYWORDS_HIDDEN_CALLABLE , true );
650
652
651
653
@ Child private ReadIndexedArgumentNode readArg1Node ;
@@ -659,7 +661,7 @@ protected Object[] prepareCArguments(VirtualFrame frame) {
659
661
return new Object []{getSelf (frame ), getArg1 (frame )};
660
662
}
661
663
662
- private Object getArg1 (VirtualFrame frame ) {
664
+ protected Object getArg1 (VirtualFrame frame ) {
663
665
if (readArg1Node == null ) {
664
666
CompilerDirectives .transferToInterpreterAndInvalidate ();
665
667
readArg1Node = insert (ReadIndexedArgumentNode .create (1 ));
@@ -673,6 +675,58 @@ public Signature getSignature() {
673
675
}
674
676
}
675
677
678
+ /**
679
+ * Implements semantics of {@code typeobject.c: wrap_sq_item}.
680
+ */
681
+ static final class HPyMethSqItemWrapperRoot extends HPyMethSSizeArgFuncRoot {
682
+
683
+ @ Child private GetIndexNode getIndexNode ;
684
+
685
+ public HPyMethSqItemWrapperRoot (PythonLanguage language , String name ) {
686
+ super (language , name );
687
+ }
688
+
689
+ @ Override
690
+ protected Object [] prepareCArguments (VirtualFrame frame ) {
691
+ Object self = getSelf (frame );
692
+ return new Object []{self , getIndex (self , getArg1 (frame ))};
693
+ }
694
+
695
+ private int getIndex (Object self , Object index ) {
696
+ if (getIndexNode == null ) {
697
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
698
+ getIndexNode = insert (GetIndexNode .create ());
699
+ }
700
+ return getIndexNode .execute (self , index );
701
+ }
702
+ }
703
+
704
+ /**
705
+ * Implements semantics of {@code typeobject.c: wrap_sq_setitem}.
706
+ */
707
+ static final class HPyMethSqSetitemWrapperRoot extends HPyMethSSizeObjArgProcRoot {
708
+
709
+ @ Child private GetIndexNode getIndexNode ;
710
+
711
+ public HPyMethSqSetitemWrapperRoot (PythonLanguage language , String name ) {
712
+ super (language , name );
713
+ }
714
+
715
+ @ Override
716
+ protected Object [] prepareCArguments (VirtualFrame frame ) {
717
+ Object self = getSelf (frame );
718
+ return new Object []{self , getIndex (self , getArg1 (frame )), getArg2 (frame )};
719
+ }
720
+
721
+ private int getIndex (Object self , Object index ) {
722
+ if (getIndexNode == null ) {
723
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
724
+ getIndexNode = insert (GetIndexNode .create ());
725
+ }
726
+ return getIndexNode .execute (self , index );
727
+ }
728
+ }
729
+
676
730
static final class HPyMethSSizeSSizeArgFuncRoot extends HPyMethodDescriptorRootNode {
677
731
private static final Signature SIGNATURE = new Signature (3 , false , -1 , false , new String []{"$self" , "n" , "m" }, KEYWORDS_HIDDEN_CALLABLE , true );
678
732
@@ -771,7 +825,7 @@ public Signature getSignature() {
771
825
}
772
826
}
773
827
774
- static final class HPyMethSSizeObjArgProcRoot extends HPyMethodDescriptorRootNode {
828
+ static class HPyMethSSizeObjArgProcRoot extends HPyMethodDescriptorRootNode {
775
829
private static final Signature SIGNATURE = new Signature (3 , false , -1 , false , new String []{"$self" , "arg0" , "arg1" }, KEYWORDS_HIDDEN_CALLABLE , true );
776
830
777
831
@ Child private ReadIndexedArgumentNode readArg1Node ;
@@ -786,15 +840,15 @@ protected Object[] prepareCArguments(VirtualFrame frame) {
786
840
return new Object []{getSelf (frame ), getArg1 (frame ), getArg2 (frame )};
787
841
}
788
842
789
- private Object getArg1 (VirtualFrame frame ) {
843
+ protected Object getArg1 (VirtualFrame frame ) {
790
844
if (readArg1Node == null ) {
791
845
CompilerDirectives .transferToInterpreterAndInvalidate ();
792
846
readArg1Node = insert (ReadIndexedArgumentNode .create (1 ));
793
847
}
794
848
return readArg1Node .execute (frame );
795
849
}
796
850
797
- private Object getArg2 (VirtualFrame frame ) {
851
+ protected Object getArg2 (VirtualFrame frame ) {
798
852
if (readArg2Node == null ) {
799
853
CompilerDirectives .transferToInterpreterAndInvalidate ();
800
854
readArg2Node = insert (ReadIndexedArgumentNode .create (2 ));
@@ -1040,4 +1094,35 @@ Object doObject(PythonContext context, @SuppressWarnings("unused") GraalHPyConte
1040
1094
throw raiseNode .raise (SystemError , "function '%s' did not return an integer." , name );
1041
1095
}
1042
1096
}
1097
+
1098
+ /**
1099
+ * Implements semantics of function {@code typeobject.c: getindex}.
1100
+ */
1101
+ static final class GetIndexNode extends Node {
1102
+
1103
+ @ Child private PythonObjectLibrary indexLib = PythonObjectLibrary .getFactory ().createDispatched (3 );
1104
+ @ Child private PythonObjectLibrary selfLib ;
1105
+ @ Child private NormalizeIndexNode normalizeIndexNode ;
1106
+
1107
+ public int execute (Object self , Object indexObj ) {
1108
+ int index = indexLib .asSize (indexObj );
1109
+ if (index < 0 ) {
1110
+ // 'selfLib' acts as an implicit profile for 'index < 0'
1111
+ if (selfLib == null ) {
1112
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1113
+ selfLib = insert (PythonObjectLibrary .getFactory ().createDispatched (1 ));
1114
+ }
1115
+ if (normalizeIndexNode == null ) {
1116
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1117
+ normalizeIndexNode = insert (NormalizeIndexNode .create (false ));
1118
+ }
1119
+ return normalizeIndexNode .execute (index , selfLib .length (self ));
1120
+ }
1121
+ return index ;
1122
+ }
1123
+
1124
+ public static GetIndexNode create () {
1125
+ return new GetIndexNode ();
1126
+ }
1127
+ }
1043
1128
}
0 commit comments