26
26
27
27
package com .oracle .graal .python .builtins .objects .code ;
28
28
29
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__HASH__ ;
30
+ import static com .oracle .graal .python .nodes .SpecialMethodNames .__REPR__ ;
31
+
29
32
import java .util .List ;
30
33
31
34
import com .oracle .graal .python .builtins .Builtin ;
32
35
import com .oracle .graal .python .builtins .CoreFunctions ;
33
36
import com .oracle .graal .python .builtins .PythonBuiltinClassType ;
34
37
import com .oracle .graal .python .builtins .PythonBuiltins ;
35
38
import com .oracle .graal .python .builtins .objects .PNone ;
39
+ import com .oracle .graal .python .builtins .objects .object .PythonObjectLibrary ;
36
40
import 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 ;
38
43
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
39
44
import com .oracle .truffle .api .dsl .GenerateNodeFactory ;
40
45
import com .oracle .truffle .api .dsl .NodeFactory ;
41
46
import com .oracle .truffle .api .dsl .Specialization ;
47
+ import com .oracle .truffle .api .library .CachedLibrary ;
42
48
43
49
@ CoreFunctions (extendClasses = PythonBuiltinClassType .PCode )
44
50
public class CodeBuiltins extends PythonBuiltins {
@@ -50,33 +56,25 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
50
56
51
57
@ Builtin (name = "co_freevars" , minNumOfPositionalArgs = 1 , isGetter = true )
52
58
@ GenerateNodeFactory
53
- public abstract static class GetFreeVarsNode extends PythonBuiltinNode {
59
+ public abstract static class GetFreeVarsNode extends PythonUnaryBuiltinNode {
54
60
@ Specialization
55
61
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 ());
61
63
}
62
64
}
63
65
64
66
@ Builtin (name = "co_cellvars" , minNumOfPositionalArgs = 1 , isGetter = true )
65
67
@ GenerateNodeFactory
66
- public abstract static class GetCellVarsNode extends PythonBuiltinNode {
68
+ public abstract static class GetCellVarsNode extends PythonUnaryBuiltinNode {
67
69
@ Specialization
68
70
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 ());
74
72
}
75
73
}
76
74
77
75
@ Builtin (name = "co_filename" , minNumOfPositionalArgs = 1 , isGetter = true )
78
76
@ GenerateNodeFactory
79
- public abstract static class GetFilenameNode extends PythonBuiltinNode {
77
+ public abstract static class GetFilenameNode extends PythonUnaryBuiltinNode {
80
78
@ Specialization
81
79
protected Object get (PCode self ) {
82
80
String filename = self .getFilename ();
@@ -89,7 +87,7 @@ protected Object get(PCode self) {
89
87
90
88
@ Builtin (name = "co_firstlineno" , minNumOfPositionalArgs = 1 , isGetter = true )
91
89
@ GenerateNodeFactory
92
- public abstract static class GetLinenoNode extends PythonBuiltinNode {
90
+ public abstract static class GetLinenoNode extends PythonUnaryBuiltinNode {
93
91
@ Specialization
94
92
protected Object get (PCode self ) {
95
93
return self .getFirstLineNo ();
@@ -98,57 +96,53 @@ protected Object get(PCode self) {
98
96
99
97
@ Builtin (name = "co_name" , minNumOfPositionalArgs = 1 , isGetter = true )
100
98
@ GenerateNodeFactory
101
- public abstract static class GetNameNode extends PythonBuiltinNode {
99
+ public abstract static class GetNameNode extends PythonUnaryBuiltinNode {
102
100
@ Specialization
103
101
@ TruffleBoundary
104
102
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 ();
110
104
}
111
105
}
112
106
113
107
@ Builtin (name = "co_argcount" , minNumOfPositionalArgs = 1 , isGetter = true )
114
108
@ GenerateNodeFactory
115
- public abstract static class GetArgCountNode extends PythonBuiltinNode {
109
+ public abstract static class GetArgCountNode extends PythonUnaryBuiltinNode {
116
110
@ Specialization
117
111
protected Object get (PCode self ) {
118
- return self .getArgcount ();
112
+ return self .co_argcount ();
119
113
}
120
114
}
121
115
122
116
@ Builtin (name = "co_posonlyargcount" , minNumOfPositionalArgs = 1 , isGetter = true )
123
117
@ GenerateNodeFactory
124
- public abstract static class GetPosOnlyArgCountNode extends PythonBuiltinNode {
118
+ public abstract static class GetPosOnlyArgCountNode extends PythonUnaryBuiltinNode {
125
119
@ Specialization
126
120
protected Object get (PCode self ) {
127
- return self .getPositionalOnlyArgCount ();
121
+ return self .co_posonlyargcount ();
128
122
}
129
123
}
130
124
131
125
@ Builtin (name = "co_kwonlyargcount" , minNumOfPositionalArgs = 1 , isGetter = true )
132
126
@ GenerateNodeFactory
133
- public abstract static class GetKnownlyArgCountNode extends PythonBuiltinNode {
127
+ public abstract static class GetKnownlyArgCountNode extends PythonUnaryBuiltinNode {
134
128
@ Specialization
135
129
protected Object get (PCode self ) {
136
- return self .getKwonlyargcount ();
130
+ return self .co_kwonlyargcount ();
137
131
}
138
132
}
139
133
140
134
@ Builtin (name = "co_nlocals" , minNumOfPositionalArgs = 1 , isGetter = true )
141
135
@ GenerateNodeFactory
142
- public abstract static class GetNLocalsNode extends PythonBuiltinNode {
136
+ public abstract static class GetNLocalsNode extends PythonUnaryBuiltinNode {
143
137
@ Specialization
144
138
protected Object get (PCode self ) {
145
- return self .getNlocals ();
139
+ return self .co_nlocals ();
146
140
}
147
141
}
148
142
149
143
@ Builtin (name = "co_stacksize" , minNumOfPositionalArgs = 1 , isGetter = true )
150
144
@ GenerateNodeFactory
151
- public abstract static class GetStackSizeNode extends PythonBuiltinNode {
145
+ public abstract static class GetStackSizeNode extends PythonUnaryBuiltinNode {
152
146
@ Specialization
153
147
protected Object get (PCode self ) {
154
148
return self .getStacksize ();
@@ -157,68 +151,52 @@ protected Object get(PCode self) {
157
151
158
152
@ Builtin (name = "co_flags" , minNumOfPositionalArgs = 1 , isGetter = true )
159
153
@ GenerateNodeFactory
160
- public abstract static class GetFlagsNode extends PythonBuiltinNode {
154
+ public abstract static class GetFlagsNode extends PythonUnaryBuiltinNode {
161
155
@ Specialization
162
156
protected Object get (PCode self ) {
163
- return self .getFlags ();
157
+ return self .co_flags ();
164
158
}
165
159
}
166
160
167
161
@ Builtin (name = "co_code" , minNumOfPositionalArgs = 1 , isGetter = true )
168
162
@ GenerateNodeFactory
169
- public abstract static class GetCodeNode extends PythonBuiltinNode {
163
+ public abstract static class GetCodeNode extends PythonUnaryBuiltinNode {
170
164
@ Specialization
171
165
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 ());
177
167
}
178
168
}
179
169
180
170
@ Builtin (name = "co_consts" , minNumOfPositionalArgs = 1 , isGetter = true )
181
171
@ GenerateNodeFactory
182
- public abstract static class GetConstsNode extends PythonBuiltinNode {
172
+ public abstract static class GetConstsNode extends PythonUnaryBuiltinNode {
183
173
@ Specialization
184
174
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 ());
190
176
}
191
177
}
192
178
193
179
@ Builtin (name = "co_names" , minNumOfPositionalArgs = 1 , isGetter = true )
194
180
@ GenerateNodeFactory
195
- public abstract static class GetNamesNode extends PythonBuiltinNode {
181
+ public abstract static class GetNamesNode extends PythonUnaryBuiltinNode {
196
182
@ Specialization
197
183
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 ());
203
185
}
204
186
}
205
187
206
188
@ Builtin (name = "co_varnames" , minNumOfPositionalArgs = 1 , isGetter = true )
207
189
@ GenerateNodeFactory
208
- public abstract static class GetVarNamesNode extends PythonBuiltinNode {
190
+ public abstract static class GetVarNamesNode extends PythonUnaryBuiltinNode {
209
191
@ Specialization
210
192
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 ());
216
194
}
217
195
}
218
196
219
197
@ Builtin (name = "co_lnotab" , minNumOfPositionalArgs = 1 , isGetter = true )
220
198
@ GenerateNodeFactory
221
- public abstract static class GetLNoTabNode extends PythonBuiltinNode {
199
+ public abstract static class GetLNoTabNode extends PythonUnaryBuiltinNode {
222
200
@ Specialization
223
201
protected Object get (PCode self ) {
224
202
byte [] lnotab = self .getLnotab ();
@@ -229,4 +207,40 @@ protected Object get(PCode self) {
229
207
return factory ().createBytes (lnotab );
230
208
}
231
209
}
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
+ }
232
246
}
0 commit comments