Skip to content

Commit c863190

Browse files
committed
PCode: add hash builtin
1 parent aacc44d commit c863190

File tree

2 files changed

+125
-40
lines changed

2 files changed

+125
-40
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/CodeBuiltins.java

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
package com.oracle.graal.python.builtins.objects.code;
2828

29+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
2930
import static com.oracle.graal.python.nodes.SpecialMethodNames.__REPR__;
3031

3132
import java.util.List;
@@ -35,12 +36,15 @@
3536
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3637
import com.oracle.graal.python.builtins.PythonBuiltins;
3738
import com.oracle.graal.python.builtins.objects.PNone;
39+
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
3840
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
3941
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
42+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4043
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4144
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
4245
import com.oracle.truffle.api.dsl.NodeFactory;
4346
import com.oracle.truffle.api.dsl.Specialization;
47+
import com.oracle.truffle.api.library.CachedLibrary;
4448

4549
@CoreFunctions(extendClasses = PythonBuiltinClassType.PCode)
4650
public 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
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@
4848

4949
import com.oracle.graal.python.PythonLanguage;
5050
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
51+
import com.oracle.graal.python.builtins.objects.PNone;
52+
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
53+
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
5154
import com.oracle.graal.python.builtins.objects.function.Signature;
5255
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
56+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5357
import com.oracle.graal.python.nodes.ModuleRootNode;
5458
import com.oracle.graal.python.nodes.PClosureFunctionRootNode;
5559
import com.oracle.graal.python.nodes.PClosureRootNode;
@@ -64,6 +68,7 @@
6468
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
6569
import com.oracle.graal.python.nodes.literal.SimpleLiteralNode;
6670
import com.oracle.graal.python.runtime.PythonCodeSerializer;
71+
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
6772
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6873
import com.oracle.truffle.api.RootCallTarget;
6974
import com.oracle.truffle.api.interop.UnsupportedMessageException;
@@ -496,4 +501,80 @@ public String toString() {
496501
int firstLineNo = this.getFirstLineNo() == 0 ? -1 : this.getFirstLineNo();
497502
return String.format("<code object %s, file \"%s\", line %d>", name, filename, firstLineNo);
498503
}
504+
505+
public Object co_name() {
506+
String name = this.getName();
507+
if (name != null) {
508+
return name;
509+
}
510+
return PNone.NONE;
511+
}
512+
513+
public PBytes co_code(PythonObjectFactory factory) {
514+
byte[] codestring = this.getCodestring();
515+
if (codestring == null) {
516+
codestring = new byte[0];
517+
}
518+
return factory.createBytes(codestring);
519+
}
520+
521+
public PTuple co_consts(PythonObjectFactory factory) {
522+
Object[] constants = this.getConstants();
523+
if (constants == null) {
524+
constants = new Object[0];
525+
}
526+
return factory.createTuple(constants);
527+
}
528+
529+
public PTuple co_names(PythonObjectFactory factory) {
530+
Object[] names = this.getNames();
531+
if (names == null) {
532+
names = new Object[0];
533+
}
534+
return factory.createTuple(names);
535+
}
536+
537+
public PythonAbstractObject co_varnames(PythonObjectFactory factory) {
538+
Object[] varNames = this.getVarnames();
539+
if (varNames != null) {
540+
return factory.createTuple(varNames);
541+
}
542+
return PNone.NONE;
543+
}
544+
545+
public PythonAbstractObject co_freevars(PythonObjectFactory factory) {
546+
Object[] freeVars = this.getFreeVars();
547+
if (freeVars != null) {
548+
return factory.createTuple(freeVars);
549+
}
550+
return PNone.NONE;
551+
}
552+
553+
public PythonAbstractObject co_cellvars(PythonObjectFactory factory) {
554+
Object[] cellVars = this.getCellVars();
555+
if (cellVars != null) {
556+
return factory.createTuple(cellVars);
557+
}
558+
return PNone.NONE;
559+
}
560+
561+
public int co_argcount() {
562+
return this.getArgcount();
563+
}
564+
565+
public int co_posonlyargcount() {
566+
return this.getPositionalOnlyArgCount();
567+
}
568+
569+
public int co_kwonlyargcount() {
570+
return this.getKwonlyargcount();
571+
}
572+
573+
public int co_nlocals() {
574+
return this.getNlocals();
575+
}
576+
577+
public int co_flags() {
578+
return this.getFlags();
579+
}
499580
}

0 commit comments

Comments
 (0)