Skip to content

Commit fd4e38f

Browse files
timfelcosminbasca
authored andcommitted
implement co_constants
1 parent 9c54885 commit fd4e38f

File tree

10 files changed

+69
-11
lines changed

10 files changed

+69
-11
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import com.oracle.graal.python.nodes.frame.WriteGlobalNode;
6262
import com.oracle.graal.python.nodes.function.FunctionRootNode;
6363
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
64+
import com.oracle.graal.python.nodes.literal.SimpleLiteralNode;
6465
import com.oracle.truffle.api.CompilerDirectives;
6566
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6667
import com.oracle.truffle.api.RootCallTarget;
@@ -251,6 +252,11 @@ private static Object[] extractVarnames(RootNode rootNode, String[] parameterIds
251252
return varNameList.toArray();
252253
}
253254

255+
@TruffleBoundary
256+
private static Object[] extractConstants(RootNode rootNode) {
257+
return NodeUtil.findAllNodeInstances(rootNode, SimpleLiteralNode.class).stream().map((n) -> n.getValue()).toArray();
258+
}
259+
254260
@TruffleBoundary
255261
private static Object[] extractGlobalAndBuiltinVarnames(RootNode rootNode) {
256262
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
@@ -407,6 +413,9 @@ public Object[] getGlobalAndBuiltinVarNames() {
407413
}
408414

409415
public Object[] getConstants() {
416+
if (constants == null) {
417+
constants = extractConstants(getRootNode());
418+
}
410419
return constants;
411420
}
412421

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/BooleanLiteralNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import com.oracle.truffle.api.frame.VirtualFrame;
2929

30-
public final class BooleanLiteralNode extends LiteralNode {
30+
public final class BooleanLiteralNode extends SimpleLiteralNode {
3131

3232
private final boolean value;
3333

@@ -44,4 +44,9 @@ public boolean executeBoolean(VirtualFrame frame) {
4444
public Object execute(VirtualFrame frame) {
4545
return value;
4646
}
47+
48+
@Override
49+
public Object getValue() {
50+
return value;
51+
}
4752
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/BytesLiteralNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
3232
import com.oracle.truffle.api.frame.VirtualFrame;
3333

34-
public final class BytesLiteralNode extends LiteralNode {
34+
public final class BytesLiteralNode extends SimpleLiteralNode {
3535
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
3636
@CompilationFinal(dimensions = 1) private final byte[] value;
3737

@@ -41,6 +41,11 @@ public BytesLiteralNode(byte[] value) {
4141

4242
@Override
4343
public Object execute(VirtualFrame frame) {
44+
return getValue();
45+
}
46+
47+
@Override
48+
public Object getValue() {
4449
return factory.createBytes(Arrays.copyOf(value, value.length));
4550
}
4651
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/ComplexLiteralNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828
import com.oracle.graal.python.builtins.objects.complex.PComplex;
2929
import com.oracle.truffle.api.frame.VirtualFrame;
3030

31-
public final class ComplexLiteralNode extends LiteralNode {
31+
public final class ComplexLiteralNode extends SimpleLiteralNode {
3232
private final PComplex value;
3333

3434
public ComplexLiteralNode(PComplex value) {
3535
this.value = value;
3636
}
3737

38+
@Override
3839
public PComplex getValue() {
3940
return value;
4041
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/DoubleLiteralNode.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import com.oracle.truffle.api.frame.VirtualFrame;
2929

30-
public final class DoubleLiteralNode extends LiteralNode {
30+
public final class DoubleLiteralNode extends SimpleLiteralNode {
3131

3232
private final double value;
3333

@@ -44,4 +44,9 @@ public double executeDouble(VirtualFrame frame) {
4444
public Object execute(VirtualFrame frame) {
4545
return value;
4646
}
47+
48+
@Override
49+
public Object getValue() {
50+
return value;
51+
}
4752
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/IntegerLiteralNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import com.oracle.truffle.api.frame.VirtualFrame;
2929

30-
public final class IntegerLiteralNode extends LiteralNode {
30+
public final class IntegerLiteralNode extends SimpleLiteralNode {
3131

3232
private final int value;
3333

@@ -45,8 +45,8 @@ public Object execute(VirtualFrame frame) {
4545
return value;
4646
}
4747

48-
public int getValue() {
48+
@Override
49+
public Integer getValue() {
4950
return value;
5051
}
51-
5252
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/LongLiteralNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import com.oracle.truffle.api.frame.VirtualFrame;
2929

30-
public final class LongLiteralNode extends LiteralNode {
30+
public final class LongLiteralNode extends SimpleLiteralNode {
3131

3232
private final long value;
3333

@@ -45,7 +45,8 @@ public Object execute(VirtualFrame frame) {
4545
return value;
4646
}
4747

48-
public long getValue() {
48+
@Override
49+
public Long getValue() {
4950
return value;
5051
}
5152
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/PIntLiteralNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@
3131
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
3232
import com.oracle.truffle.api.frame.VirtualFrame;
3333

34-
public final class PIntLiteralNode extends LiteralNode {
34+
public final class PIntLiteralNode extends SimpleLiteralNode {
3535
private final PInt value;
3636

3737
public PIntLiteralNode(BigInteger value) {
3838
this.value = PythonObjectFactory.getUncached().createInt(value);
3939
}
4040

41+
@Override
4142
public PInt getValue() {
4243
return value;
4344
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2017, 2018, Oracle and/or its affiliates.
3+
* Copyright (c) 2013, Regents of the University of California
4+
*
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification, are
8+
* permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice, this list of
11+
* conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+
* conditions and the following disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
17+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
24+
* OF THE POSSIBILITY OF SUCH DAMAGE.
25+
*/
26+
package com.oracle.graal.python.nodes.literal;
27+
28+
public abstract class SimpleLiteralNode extends LiteralNode {
29+
public abstract Object getValue();
30+
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/literal/StringLiteralNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import com.oracle.truffle.api.frame.VirtualFrame;
2929

30-
public final class StringLiteralNode extends LiteralNode {
30+
public final class StringLiteralNode extends SimpleLiteralNode {
3131

3232
private final String value;
3333

@@ -40,6 +40,7 @@ public Object execute(VirtualFrame frame) {
4040
return value;
4141
}
4242

43+
@Override
4344
public String getValue() {
4445
return value;
4546
}

0 commit comments

Comments
 (0)