11/*
2- * Copyright (c) 2021, 2022 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2021, 2023 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * The Universal Permissive License (UPL), Version 1.0
4545import static com .oracle .graal .python .util .PythonUtils .TS_ENCODING ;
4646
4747import com .oracle .graal .python .builtins .objects .PNone ;
48+ import com .oracle .graal .python .builtins .objects .bytes .BytesBuiltins ;
49+ import com .oracle .graal .python .builtins .objects .bytes .PBytes ;
4850import com .oracle .graal .python .builtins .objects .common .HashingStorageNodes .HashingStorageLen ;
4951import com .oracle .graal .python .builtins .objects .dict .PDict ;
5052import com .oracle .graal .python .builtins .objects .list .PList ;
5961import com .oracle .graal .python .nodes .PRaiseNode ;
6062import com .oracle .graal .python .nodes .call .special .CallUnaryMethodNode ;
6163import com .oracle .graal .python .nodes .call .special .LookupSpecialMethodSlotNode ;
62- import com .oracle .graal .python .nodes .object .GetClassNode ;
64+ import com .oracle .graal .python .nodes .object .InlinedGetClassNode ;
65+ import com .oracle .graal .python .nodes .object .InlinedGetClassNode .GetPythonObjectClassNode ;
6366import com .oracle .graal .python .nodes .util .CastToJavaIntLossyNode ;
6467import com .oracle .graal .python .runtime .exception .PException ;
68+ import com .oracle .truffle .api .dsl .Bind ;
6569import com .oracle .truffle .api .dsl .Cached ;
6670import com .oracle .truffle .api .dsl .Cached .Shared ;
6771import com .oracle .truffle .api .dsl .Fallback ;
@@ -94,39 +98,51 @@ static int doTruffleString(TruffleString str,
9498 return codePointLengthNode .execute (str , TS_ENCODING );
9599 }
96100
97- @ Specialization (guards = "cannotBeOverridden(object, getClassNode)" , limit = "1" )
101+ @ Specialization (guards = "cannotBeOverridden(object, inliningTarget, getClassNode)" , limit = "1" )
98102 static int doList (PList object ,
99- @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ) {
103+ @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
104+ @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetPythonObjectClassNode getClassNode ) {
100105 return object .getSequenceStorage ().length ();
101106 }
102107
103- @ Specialization (guards = "cannotBeOverridden(object, getClassNode)" , limit = "1" )
108+ @ Specialization (guards = "cannotBeOverridden(object, inliningTarget, getClassNode)" , limit = "1" )
104109 static int doTuple (PTuple object ,
105- @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ) {
110+ @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
111+ @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetPythonObjectClassNode getClassNode ) {
106112 return object .getSequenceStorage ().length ();
107113 }
108114
109- @ Specialization (guards = "cannotBeOverridden(object, getClassNode)" , limit = "1" )
115+ @ Specialization (guards = "cannotBeOverridden(object, inliningTarget, getClassNode)" , limit = "1" )
110116 static int doDict (PDict object ,
111- @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
117+ @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
118+ @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetPythonObjectClassNode getClassNode ,
112119 @ Shared ("hashingStorageLen" ) @ Cached HashingStorageLen lenNode ) {
113120 return lenNode .execute (object .getDictStorage ());
114121 }
115122
116- @ Specialization (guards = "cannotBeOverridden(object, getClassNode)" , limit = "1" )
123+ @ Specialization (guards = "cannotBeOverridden(object, inliningTarget, getClassNode)" , limit = "1" )
117124 static int doSet (PSet object ,
118- @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
125+ @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
126+ @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetPythonObjectClassNode getClassNode ,
119127 @ Shared ("hashingStorageLen" ) @ Cached HashingStorageLen lenNode ) {
120128 return lenNode .execute (object .getDictStorage ());
121129 }
122130
123- @ Specialization (guards = "cannotBeOverridden(object, getClassNode)" , limit = "1" )
131+ @ Specialization (guards = "cannotBeOverridden(object, inliningTarget, getClassNode)" , limit = "1" )
124132 static int doPString (PString object ,
125- @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetClassNode getClassNode ,
133+ @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
134+ @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetPythonObjectClassNode getClassNode ,
126135 @ Cached StringNodes .StringLenNode lenNode ) {
127136 return lenNode .execute (object );
128137 }
129138
139+ @ Specialization (guards = "cannotBeOverridden(object, inliningTarget, getClassNode)" , limit = "1" )
140+ static int doPBytes (PBytes object ,
141+ @ SuppressWarnings ("unused" ) @ Bind ("this" ) Node inliningTarget ,
142+ @ Shared ("getClass" ) @ SuppressWarnings ("unused" ) @ Cached GetPythonObjectClassNode getClassNode ) {
143+ return BytesBuiltins .LenNode .len (object );
144+ }
145+
130146 @ Fallback
131147 static int doOthers (VirtualFrame frame , Object object ,
132148 @ Cached PyObjectSizeGenericNode genericNode ) {
@@ -142,14 +158,15 @@ abstract static class PyObjectSizeGenericNode extends Node {
142158
143159 @ Specialization (rewriteOn = UnexpectedResultException .class )
144160 static int doInt (VirtualFrame frame , Object object ,
145- @ Shared ("getClass" ) @ Cached GetClassNode getClassNode ,
161+ @ Bind ("this" ) Node inliningTarget ,
162+ @ Shared ("getClass" ) @ Cached InlinedGetClassNode getClassNode ,
146163 @ Shared ("lookupLen" ) @ Cached (parameters = "Len" ) LookupSpecialMethodSlotNode lookupLen ,
147164 @ Shared ("callLen" ) @ Cached CallUnaryMethodNode callLen ,
148165 @ Shared ("index" ) @ Cached PyNumberIndexNode indexNode ,
149166 @ Shared ("castLossy" ) @ Cached CastToJavaIntLossyNode castLossy ,
150167 @ Shared ("asSize" ) @ Cached PyNumberAsSizeNode asSizeNode ,
151168 @ Shared ("raise" ) @ Cached PRaiseNode raiseNode ) throws UnexpectedResultException {
152- Object lenDescr = lookupLen .execute (frame , getClassNode .execute (object ), object );
169+ Object lenDescr = lookupLen .execute (frame , getClassNode .execute (inliningTarget , object ), object );
153170 if (lenDescr == PNone .NO_VALUE ) {
154171 throw raiseNode .raise (TypeError , ErrorMessages .OBJ_HAS_NO_LEN , object );
155172 }
@@ -163,14 +180,15 @@ static int doInt(VirtualFrame frame, Object object,
163180
164181 @ Specialization (replaces = "doInt" )
165182 static int doObject (VirtualFrame frame , Object object ,
166- @ Shared ("getClass" ) @ Cached GetClassNode getClassNode ,
183+ @ Bind ("this" ) Node inliningTarget ,
184+ @ Shared ("getClass" ) @ Cached InlinedGetClassNode getClassNode ,
167185 @ Shared ("lookupLen" ) @ Cached (parameters = "Len" ) LookupSpecialMethodSlotNode lookupLen ,
168186 @ Shared ("callLen" ) @ Cached CallUnaryMethodNode callLen ,
169187 @ Shared ("index" ) @ Cached PyNumberIndexNode indexNode ,
170188 @ Shared ("castLossy" ) @ Cached CastToJavaIntLossyNode castLossy ,
171189 @ Shared ("asSize" ) @ Cached PyNumberAsSizeNode asSizeNode ,
172190 @ Shared ("raise" ) @ Cached PRaiseNode raiseNode ) {
173- Object lenDescr = lookupLen .execute (frame , getClassNode .execute (object ), object );
191+ Object lenDescr = lookupLen .execute (frame , getClassNode .execute (inliningTarget , object ), object );
174192 if (lenDescr == PNone .NO_VALUE ) {
175193 throw raiseNode .raise (TypeError , ErrorMessages .OBJ_HAS_NO_LEN , object );
176194 }
0 commit comments