Skip to content

Commit 0640b9d

Browse files
committed
added TUPLE_FROM_LIST operation
1 parent 6c8249a commit 0640b9d

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

graalpython/com.oracle.graal.python.test/testData/goldenFiles/CompilerTests/testTupleLiteralExpand.co

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ Disassembly of <module>:
77
000000 10 COLLECTION_ADD_COLLECTION 0 (list)
88
000014 12 LOAD_BYTE_O 5
99
000000 14 COLLECTION_ADD_STACK 1 (list)
10-
000000 16 COLLECTION_FROM_COLLECTION 0 (tuple)
11-
000000 18 RETURN_VALUE
10+
000000 16 TUPLE_FROM_LIST
11+
000000 17 RETURN_VALUE

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/Compiler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import static com.oracle.graal.python.compiler.OpCodes.STORE_NAME;
125125
import static com.oracle.graal.python.compiler.OpCodes.STORE_SUBSCR;
126126
import static com.oracle.graal.python.compiler.OpCodes.THROW;
127+
import static com.oracle.graal.python.compiler.OpCodes.TUPLE_FROM_LIST;
127128
import static com.oracle.graal.python.compiler.OpCodes.UNARY_OP;
128129
import static com.oracle.graal.python.compiler.OpCodes.UNPACK_EX;
129130
import static com.oracle.graal.python.compiler.OpCodes.UNPACK_SEQUENCE;
@@ -1632,9 +1633,7 @@ public Void visit(ExprTy.Tuple node) {
16321633
collectIntoArray(node.elements, CollectionBits.KIND_TUPLE);
16331634
} else {
16341635
collectIntoArray(node.elements, CollectionBits.KIND_LIST);
1635-
// FIXME this operation copies the underlying storage, we should make a
1636-
// separate instruction for shallow conversion
1637-
addOp(COLLECTION_FROM_COLLECTION, CollectionBits.KIND_TUPLE);
1636+
addOp(TUPLE_FROM_LIST);
16381637
}
16391638
return null;
16401639
case Del:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/OpCodes.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,14 @@ public enum OpCodes {
547547
* Pushes: converted collection
548548
*/
549549
COLLECTION_FROM_COLLECTION(1, 1, 1),
550+
/**
551+
* Converts list to tuple by reusing the underlying storage.
552+
*
553+
* Pops: list
554+
*
555+
* Pushes: tuple
556+
*/
557+
TUPLE_FROM_LIST(0, 1, 1),
550558
/**
551559
* Adds an item to a collection that is multiple items deep under the top of the stack,
552560
* determined by the immediate argument.

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,11 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
12391239
stackTop = bytecodeAddToCollection(virtualFrame, stackTop, beginBci, localNodes, depth, type, useCachedNodes);
12401240
break;
12411241
}
1242+
case OpCodesConstants.TUPLE_FROM_LIST: {
1243+
setCurrentBci(virtualFrame, bciSlot, bci);
1244+
bytecodeTupleFromList(virtualFrame, stackTop);
1245+
break;
1246+
}
12421247
case OpCodesConstants.KWARGS_DICT_MERGE: {
12431248
setCurrentBci(virtualFrame, bciSlot, bci);
12441249
KwargsMergeNode mergeNode = insertChildNode(localNodes, bci, UNCACHED_KWARGS_MERGE, KwargsMergeNodeGen.class, NODE_KWARGS_MERGE, useCachedNodes);
@@ -4238,6 +4243,12 @@ private int bytecodeAddToCollection(VirtualFrame virtualFrame, int initialStackT
42384243
return stackTop;
42394244
}
42404245

4246+
private void bytecodeTupleFromList(VirtualFrame virtualFrame, int stackTop) {
4247+
PList list = (PList) virtualFrame.getObject(stackTop);
4248+
Object result = factory.createTuple(list.getSequenceStorage());
4249+
virtualFrame.setObject(stackTop, result);
4250+
}
4251+
42414252
private int bytecodeUnpackSequence(VirtualFrame virtualFrame, int stackTop, int bci, Node[] localNodes, int count, boolean useCachedNodes) {
42424253
UnpackSequenceNode unpackNode = insertChildNode(localNodes, bci, UNCACHED_UNPACK_SEQUENCE, UnpackSequenceNodeGen.class, NODE_UNPACK_SEQUENCE, useCachedNodes);
42434254
Object collection = virtualFrame.getObject(stackTop);

0 commit comments

Comments
 (0)