2626
2727package com .oracle .graal .python .builtins .objects .code ;
2828
29+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__HASH__ ;
2930import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
3031
3132import java .util .List ;
3536import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
3637import com .oracle .graal .python .builtins .PythonBuiltins ;
3738import com .oracle .graal .python .builtins .objects .PNone ;
39+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
3840import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
3941import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
42+ import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
4043import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
4144import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
4245import com .oracle .truffle .api .dsl .NodeFactory ;
4346import com .oracle .truffle .api .dsl .Specialization ;
47+ import com .oracle .truffle .api .library .CachedLibrary ;
4448
4549@ CoreFunctions (extendClasses = PythonBuiltinClassType .PCode )
4650public class CodeBuiltins extends PythonBuiltins {
@@ -55,11 +59,7 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
5559 public abstract static class GetFreeVarsNode extends PythonUnaryBuiltinNode {
5660 @ Specialization
5761 protected Object get (PCode self ) {
58- Object [] freeVars = self .getFreeVars ();
59- if (freeVars != null ) {
60- return factory ().createTuple (freeVars );
61- }
62- return PNone .NONE ;
62+ return self .co_freevars (factory ());
6363 }
6464 }
6565
@@ -68,11 +68,7 @@ protected Object get(PCode self) {
6868 public abstract static class GetCellVarsNode extends PythonUnaryBuiltinNode {
6969 @ Specialization
7070 protected Object get (PCode self ) {
71- Object [] cellVars = self .getCellVars ();
72- if (cellVars != null ) {
73- return factory ().createTuple (cellVars );
74- }
75- return PNone .NONE ;
71+ return self .co_cellvars (factory ());
7672 }
7773 }
7874
@@ -104,11 +100,7 @@ public abstract static class GetNameNode extends PythonUnaryBuiltinNode {
104100 @ Specialization
105101 @ TruffleBoundary
106102 protected Object get (PCode self ) {
107- String name = self .getName ();
108- if (name != null ) {
109- return name ;
110- }
111- return PNone .NONE ;
103+ return self .co_name ();
112104 }
113105 }
114106
@@ -117,7 +109,7 @@ protected Object get(PCode self) {
117109 public abstract static class GetArgCountNode extends PythonUnaryBuiltinNode {
118110 @ Specialization
119111 protected Object get (PCode self ) {
120- return self .getArgcount ();
112+ return self .co_argcount ();
121113 }
122114 }
123115
@@ -126,7 +118,7 @@ protected Object get(PCode self) {
126118 public abstract static class GetPosOnlyArgCountNode extends PythonUnaryBuiltinNode {
127119 @ Specialization
128120 protected Object get (PCode self ) {
129- return self .getPositionalOnlyArgCount ();
121+ return self .co_posonlyargcount ();
130122 }
131123 }
132124
@@ -135,7 +127,7 @@ protected Object get(PCode self) {
135127 public abstract static class GetKnownlyArgCountNode extends PythonUnaryBuiltinNode {
136128 @ Specialization
137129 protected Object get (PCode self ) {
138- return self .getKwonlyargcount ();
130+ return self .co_kwonlyargcount ();
139131 }
140132 }
141133
@@ -144,7 +136,7 @@ protected Object get(PCode self) {
144136 public abstract static class GetNLocalsNode extends PythonUnaryBuiltinNode {
145137 @ Specialization
146138 protected Object get (PCode self ) {
147- return self .getNlocals ();
139+ return self .co_nlocals ();
148140 }
149141 }
150142
@@ -162,7 +154,7 @@ protected Object get(PCode self) {
162154 public abstract static class GetFlagsNode extends PythonUnaryBuiltinNode {
163155 @ Specialization
164156 protected Object get (PCode self ) {
165- return self .getFlags ();
157+ return self .co_flags ();
166158 }
167159 }
168160
@@ -171,11 +163,7 @@ protected Object get(PCode self) {
171163 public abstract static class GetCodeNode extends PythonUnaryBuiltinNode {
172164 @ Specialization
173165 protected Object get (PCode self ) {
174- byte [] codestring = self .getCodestring ();
175- if (codestring == null ) {
176- codestring = new byte [0 ];
177- }
178- return factory ().createBytes (codestring );
166+ return self .co_code (factory ());
179167 }
180168 }
181169
@@ -184,11 +172,7 @@ protected Object get(PCode self) {
184172 public abstract static class GetConstsNode extends PythonUnaryBuiltinNode {
185173 @ Specialization
186174 protected Object get (PCode self ) {
187- Object [] constants = self .getConstants ();
188- if (constants == null ) {
189- constants = new Object [0 ];
190- }
191- return factory ().createTuple (constants );
175+ return self .co_consts (factory ());
192176 }
193177 }
194178
@@ -197,11 +181,7 @@ protected Object get(PCode self) {
197181 public abstract static class GetNamesNode extends PythonUnaryBuiltinNode {
198182 @ Specialization
199183 protected Object get (PCode self ) {
200- Object [] names = self .getNames ();
201- if (names == null ) {
202- names = new Object [0 ];
203- }
204- return factory ().createTuple (names );
184+ return self .co_names (factory ());
205185 }
206186 }
207187
@@ -210,11 +190,7 @@ protected Object get(PCode self) {
210190 public abstract static class GetVarNamesNode extends PythonUnaryBuiltinNode {
211191 @ Specialization
212192 protected Object get (PCode self ) {
213- Object [] varNames = self .getVarnames ();
214- if (varNames != null ) {
215- return factory ().createTuple (varNames );
216- }
217- return PNone .NONE ;
193+ return self .co_varnames (factory ());
218194 }
219195 }
220196
@@ -235,8 +211,36 @@ protected Object get(PCode self) {
235211 @ Builtin (name = __REPR__ , minNumOfPositionalArgs = 1 )
236212 @ GenerateNodeFactory
237213 public abstract static class CodeReprNode extends PythonUnaryBuiltinNode {
214+ @ Specialization
238215 Object repr (PCode self ) {
239216 return self .toString ();
240217 }
241218 }
219+
220+ @ Builtin (name = __HASH__ , minNumOfPositionalArgs = 1 )
221+ @ GenerateNodeFactory
222+ public abstract static class CodeHashNode extends PythonUnaryBuiltinNode {
223+ @ Specialization
224+ long hash (PCode self ,
225+ @ CachedLibrary (limit = "getCallSiteInlineCacheMaxDepth()" ) PythonObjectLibrary pol ) {
226+ long h , h0 , h1 , h2 , h3 , h4 , h5 , h6 ;
227+ PythonObjectFactory factory = factory ();
228+
229+ h0 = pol .hash (self .co_name ());
230+ h1 = pol .hash (self .co_code (factory ));
231+ h2 = pol .hash (self .co_consts (factory ));
232+ h3 = pol .hash (self .co_names (factory ));
233+ h4 = pol .hash (self .co_varnames (factory ));
234+ h5 = pol .hash (self .co_freevars (factory ));
235+ h6 = pol .hash (self .co_cellvars (factory ));
236+
237+ h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
238+ self .co_argcount () ^ self .co_posonlyargcount () ^ self .co_kwonlyargcount () ^
239+ self .co_nlocals () ^ self .co_flags ();
240+ if (h == -1 ) {
241+ h = -2 ;
242+ }
243+ return h ;
244+ }
245+ }
242246}
0 commit comments