2626
2727package com .oracle .graal .python .builtins .objects .code ;
2828
29+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__HASH__ ;
30+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
31+
2932import java .util .List ;
3033
3134import com .oracle .graal .python .builtins .Builtin ;
3235import com .oracle .graal .python .builtins .CoreFunctions ;
3336import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
3437import com .oracle .graal .python .builtins .PythonBuiltins ;
3538import com .oracle .graal .python .builtins .objects .PNone ;
39+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
3640import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
37- import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
41+ import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
42+ import com .oracle .graal .python .runtime .object .PythonObjectFactory ;
3843import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
3944import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
4045import com .oracle .truffle .api .dsl .NodeFactory ;
4146import com .oracle .truffle .api .dsl .Specialization ;
47+ import com .oracle .truffle .api .library .CachedLibrary ;
4248
4349@ CoreFunctions (extendClasses = PythonBuiltinClassType .PCode )
4450public class CodeBuiltins extends PythonBuiltins {
@@ -50,33 +56,25 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
5056
5157 @ Builtin (name = "co_freevars" , minNumOfPositionalArgs = 1 , isGetter = true )
5258 @ GenerateNodeFactory
53- public abstract static class GetFreeVarsNode extends PythonBuiltinNode {
59+ public abstract static class GetFreeVarsNode extends PythonUnaryBuiltinNode {
5460 @ Specialization
5561 protected Object get (PCode self ) {
56- Object [] freeVars = self .getFreeVars ();
57- if (freeVars != null ) {
58- return factory ().createTuple (freeVars );
59- }
60- return PNone .NONE ;
62+ return self .co_freevars (factory ());
6163 }
6264 }
6365
6466 @ Builtin (name = "co_cellvars" , minNumOfPositionalArgs = 1 , isGetter = true )
6567 @ GenerateNodeFactory
66- public abstract static class GetCellVarsNode extends PythonBuiltinNode {
68+ public abstract static class GetCellVarsNode extends PythonUnaryBuiltinNode {
6769 @ Specialization
6870 protected Object get (PCode self ) {
69- Object [] cellVars = self .getCellVars ();
70- if (cellVars != null ) {
71- return factory ().createTuple (cellVars );
72- }
73- return PNone .NONE ;
71+ return self .co_cellvars (factory ());
7472 }
7573 }
7674
7775 @ Builtin (name = "co_filename" , minNumOfPositionalArgs = 1 , isGetter = true )
7876 @ GenerateNodeFactory
79- public abstract static class GetFilenameNode extends PythonBuiltinNode {
77+ public abstract static class GetFilenameNode extends PythonUnaryBuiltinNode {
8078 @ Specialization
8179 protected Object get (PCode self ) {
8280 String filename = self .getFilename ();
@@ -89,7 +87,7 @@ protected Object get(PCode self) {
8987
9088 @ Builtin (name = "co_firstlineno" , minNumOfPositionalArgs = 1 , isGetter = true )
9189 @ GenerateNodeFactory
92- public abstract static class GetLinenoNode extends PythonBuiltinNode {
90+ public abstract static class GetLinenoNode extends PythonUnaryBuiltinNode {
9391 @ Specialization
9492 protected Object get (PCode self ) {
9593 return self .getFirstLineNo ();
@@ -98,57 +96,53 @@ protected Object get(PCode self) {
9896
9997 @ Builtin (name = "co_name" , minNumOfPositionalArgs = 1 , isGetter = true )
10098 @ GenerateNodeFactory
101- public abstract static class GetNameNode extends PythonBuiltinNode {
99+ public abstract static class GetNameNode extends PythonUnaryBuiltinNode {
102100 @ Specialization
103101 @ TruffleBoundary
104102 protected Object get (PCode self ) {
105- String name = self .getName ();
106- if (name != null ) {
107- return name ;
108- }
109- return PNone .NONE ;
103+ return self .co_name ();
110104 }
111105 }
112106
113107 @ Builtin (name = "co_argcount" , minNumOfPositionalArgs = 1 , isGetter = true )
114108 @ GenerateNodeFactory
115- public abstract static class GetArgCountNode extends PythonBuiltinNode {
109+ public abstract static class GetArgCountNode extends PythonUnaryBuiltinNode {
116110 @ Specialization
117111 protected Object get (PCode self ) {
118- return self .getArgcount ();
112+ return self .co_argcount ();
119113 }
120114 }
121115
122116 @ Builtin (name = "co_posonlyargcount" , minNumOfPositionalArgs = 1 , isGetter = true )
123117 @ GenerateNodeFactory
124- public abstract static class GetPosOnlyArgCountNode extends PythonBuiltinNode {
118+ public abstract static class GetPosOnlyArgCountNode extends PythonUnaryBuiltinNode {
125119 @ Specialization
126120 protected Object get (PCode self ) {
127- return self .getPositionalOnlyArgCount ();
121+ return self .co_posonlyargcount ();
128122 }
129123 }
130124
131125 @ Builtin (name = "co_kwonlyargcount" , minNumOfPositionalArgs = 1 , isGetter = true )
132126 @ GenerateNodeFactory
133- public abstract static class GetKnownlyArgCountNode extends PythonBuiltinNode {
127+ public abstract static class GetKnownlyArgCountNode extends PythonUnaryBuiltinNode {
134128 @ Specialization
135129 protected Object get (PCode self ) {
136- return self .getKwonlyargcount ();
130+ return self .co_kwonlyargcount ();
137131 }
138132 }
139133
140134 @ Builtin (name = "co_nlocals" , minNumOfPositionalArgs = 1 , isGetter = true )
141135 @ GenerateNodeFactory
142- public abstract static class GetNLocalsNode extends PythonBuiltinNode {
136+ public abstract static class GetNLocalsNode extends PythonUnaryBuiltinNode {
143137 @ Specialization
144138 protected Object get (PCode self ) {
145- return self .getNlocals ();
139+ return self .co_nlocals ();
146140 }
147141 }
148142
149143 @ Builtin (name = "co_stacksize" , minNumOfPositionalArgs = 1 , isGetter = true )
150144 @ GenerateNodeFactory
151- public abstract static class GetStackSizeNode extends PythonBuiltinNode {
145+ public abstract static class GetStackSizeNode extends PythonUnaryBuiltinNode {
152146 @ Specialization
153147 protected Object get (PCode self ) {
154148 return self .getStacksize ();
@@ -157,68 +151,52 @@ protected Object get(PCode self) {
157151
158152 @ Builtin (name = "co_flags" , minNumOfPositionalArgs = 1 , isGetter = true )
159153 @ GenerateNodeFactory
160- public abstract static class GetFlagsNode extends PythonBuiltinNode {
154+ public abstract static class GetFlagsNode extends PythonUnaryBuiltinNode {
161155 @ Specialization
162156 protected Object get (PCode self ) {
163- return self .getFlags ();
157+ return self .co_flags ();
164158 }
165159 }
166160
167161 @ Builtin (name = "co_code" , minNumOfPositionalArgs = 1 , isGetter = true )
168162 @ GenerateNodeFactory
169- public abstract static class GetCodeNode extends PythonBuiltinNode {
163+ public abstract static class GetCodeNode extends PythonUnaryBuiltinNode {
170164 @ Specialization
171165 protected Object get (PCode self ) {
172- byte [] codestring = self .getCodestring ();
173- if (codestring == null ) {
174- codestring = new byte [0 ];
175- }
176- return factory ().createBytes (codestring );
166+ return self .co_code (factory ());
177167 }
178168 }
179169
180170 @ Builtin (name = "co_consts" , minNumOfPositionalArgs = 1 , isGetter = true )
181171 @ GenerateNodeFactory
182- public abstract static class GetConstsNode extends PythonBuiltinNode {
172+ public abstract static class GetConstsNode extends PythonUnaryBuiltinNode {
183173 @ Specialization
184174 protected Object get (PCode self ) {
185- Object [] constants = self .getConstants ();
186- if (constants == null ) {
187- constants = new Object [0 ];
188- }
189- return factory ().createTuple (constants );
175+ return self .co_consts (factory ());
190176 }
191177 }
192178
193179 @ Builtin (name = "co_names" , minNumOfPositionalArgs = 1 , isGetter = true )
194180 @ GenerateNodeFactory
195- public abstract static class GetNamesNode extends PythonBuiltinNode {
181+ public abstract static class GetNamesNode extends PythonUnaryBuiltinNode {
196182 @ Specialization
197183 protected Object get (PCode self ) {
198- Object [] names = self .getNames ();
199- if (names == null ) {
200- names = new Object [0 ];
201- }
202- return factory ().createTuple (names );
184+ return self .co_names (factory ());
203185 }
204186 }
205187
206188 @ Builtin (name = "co_varnames" , minNumOfPositionalArgs = 1 , isGetter = true )
207189 @ GenerateNodeFactory
208- public abstract static class GetVarNamesNode extends PythonBuiltinNode {
190+ public abstract static class GetVarNamesNode extends PythonUnaryBuiltinNode {
209191 @ Specialization
210192 protected Object get (PCode self ) {
211- Object [] varNames = self .getVarnames ();
212- if (varNames != null ) {
213- return factory ().createTuple (varNames );
214- }
215- return PNone .NONE ;
193+ return self .co_varnames (factory ());
216194 }
217195 }
218196
219197 @ Builtin (name = "co_lnotab" , minNumOfPositionalArgs = 1 , isGetter = true )
220198 @ GenerateNodeFactory
221- public abstract static class GetLNoTabNode extends PythonBuiltinNode {
199+ public abstract static class GetLNoTabNode extends PythonUnaryBuiltinNode {
222200 @ Specialization
223201 protected Object get (PCode self ) {
224202 byte [] lnotab = self .getLnotab ();
@@ -229,4 +207,40 @@ protected Object get(PCode self) {
229207 return factory ().createBytes (lnotab );
230208 }
231209 }
210+
211+ @ Builtin (name = __REPR__ , minNumOfPositionalArgs = 1 )
212+ @ GenerateNodeFactory
213+ public abstract static class CodeReprNode extends PythonUnaryBuiltinNode {
214+ @ Specialization
215+ Object repr (PCode self ) {
216+ return self .toString ();
217+ }
218+ }
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+ }
232246}
0 commit comments