Skip to content

Commit d74573b

Browse files
committed
Migrate more operations to ConstantOperand; use consistent parameter names for dynamic operands
1 parent 09af687 commit d74573b

File tree

2 files changed

+243
-253
lines changed

2 files changed

+243
-253
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

Lines changed: 54 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -882,20 +882,17 @@ private void emitNameCellOperation(String mangled, NameOperation op, Builder b)
882882
switch (op) {
883883
case Read:
884884
if (scope.isClass()) {
885-
b.beginClassLoadCell();
886-
b.emitLoadConstant(index);
885+
b.beginClassLoadCell(index);
887886
b.emitLoadLocal(local);
888887
b.endClassLoadCell();
889888
} else {
890-
b.beginLoadCell();
891-
b.emitLoadConstant(index);
889+
b.beginLoadCell(index);
892890
b.emitLoadLocal(local);
893891
b.endLoadCell();
894892
}
895893
break;
896894
case Delete:
897-
b.beginClearCell();
898-
b.emitLoadConstant(index);
895+
b.beginClearCell(index);
899896
b.emitLoadLocal(local);
900897
b.endClearCell();
901898
break;
@@ -915,15 +912,13 @@ private void emitNameFastOperation(String mangled, NameOperation op, Builder b)
915912
BytecodeLocal local = locals.get(mangled);
916913
switch (op) {
917914
case Read:
918-
b.beginCheckUnboundLocal();
919-
b.emitLoadConstant(varnames.get(mangled));
915+
b.beginCheckUnboundLocal(varnames.get(mangled));
920916
b.emitLoadLocal(local);
921917
b.endCheckUnboundLocal();
922918
break;
923919
case Delete:
924920
b.beginBlock();
925-
b.beginCheckUnboundLocal();
926-
b.emitLoadConstant(varnames.get(mangled));
921+
b.beginCheckUnboundLocal(varnames.get(mangled));
927922
b.emitLoadLocal(local);
928923
b.endCheckUnboundLocal();
929924

@@ -952,20 +947,15 @@ private void emitNameGlobalOperation(String name, NameOperation op, Builder b) {
952947
TruffleString tsName = toTruffleStringUncached(name);
953948
switch (op) {
954949
case Read:
955-
b.beginReadGlobal();
956-
b.emitLoadConstant(tsName);
957-
b.endReadGlobal();
950+
b.emitReadGlobal(tsName);
958951
break;
959952
case Delete:
960-
b.beginDeleteGlobal();
961-
b.emitLoadConstant(tsName);
962-
b.endDeleteGlobal();
953+
b.emitDeleteGlobal(tsName);
963954
break;
964955
case BeginWrite:
965-
b.beginWriteGlobal();
956+
b.beginWriteGlobal(tsName);
966957
break;
967958
case EndWrite:
968-
b.emitLoadConstant(tsName);
969959
b.endWriteGlobal();
970960
break;
971961
default:
@@ -979,20 +969,15 @@ private void emitNameSlowOperation(String name, NameOperation op, Builder b) {
979969
TruffleString tsName = toTruffleStringUncached(name);
980970
switch (op) {
981971
case Read:
982-
b.beginReadName();
983-
b.emitLoadConstant(tsName);
984-
b.endReadName();
972+
b.emitReadName(tsName);
985973
break;
986974
case Delete:
987-
b.beginDeleteName();
988-
b.emitLoadConstant(tsName);
989-
b.endDeleteName();
975+
b.emitDeleteName(tsName);
990976
break;
991977
case BeginWrite:
992-
b.beginWriteName();
978+
b.beginWriteName(tsName);
993979
break;
994980
case EndWrite:
995-
b.emitLoadConstant(tsName);
996981
b.endWriteName();
997982
break;
998983
default:
@@ -1482,10 +1467,9 @@ private void emitGetMethod(ExprTy func, BytecodeLocal receiver) {
14821467
attrAccess.value.accept(this);
14831468
b.endStoreLocal();
14841469

1485-
b.beginGetMethod();
1486-
b.emitLoadLocal(receiver);
14871470
String mangled = mangle(attrAccess.attr);
1488-
b.emitLoadConstant(toTruffleStringUncached(mangled));
1471+
b.beginGetMethod(toTruffleStringUncached(mangled));
1472+
b.emitLoadLocal(receiver);
14891473
b.endGetMethod();
14901474
b.endBlock();
14911475
}
@@ -1634,23 +1618,21 @@ private void createConstant(ConstantValue value) {
16341618
emitPythonConstant(value.getDouble(), b);
16351619
break;
16361620
case COMPLEX: {
1637-
b.beginLoadComplex();
1638-
emitPythonConstant(value.getComplex(), b);
1639-
b.endLoadComplex();
1621+
double[] complex = value.getComplex();
1622+
addConstant(complex);
1623+
b.emitLoadComplex(complex[0], complex[1]);
16401624
break;
16411625
}
16421626
case BIGINTEGER:
1643-
b.beginLoadBigInt();
1644-
emitPythonConstant(value.getBigInteger(), b);
1645-
b.endLoadBigInt();
1627+
addConstant(value.getBigInteger());
1628+
b.emitLoadBigInt(value.getBigInteger());
16461629
break;
16471630
case RAW:
16481631
emitPythonConstant(value.getRaw(TruffleString.class), b);
16491632
break;
16501633
case BYTES:
1651-
b.beginLoadBytes();
1652-
emitPythonConstant(value.getBytes(), b);
1653-
b.endLoadBytes();
1634+
addConstant(value.getBytes());
1635+
b.emitLoadBytes(value.getBytes());
16541636
break;
16551637
case TUPLE:
16561638
b.beginMakeTuple();
@@ -1882,63 +1864,45 @@ public Void visit(ExprTy.NamedExpr node) {
18821864
}
18831865

18841866
private void emitConstantList(ConstantCollection constantCollection) {
1867+
addConstant(constantCollection.collection);
18851868
switch (constantCollection.elementType) {
18861869
case CollectionBits.ELEMENT_INT:
1887-
b.beginMakeConstantIntList();
1888-
emitPythonConstant(constantCollection.collection, b);
1889-
b.endMakeConstantIntList();
1870+
b.emitMakeConstantIntList((int[]) constantCollection.collection);
18901871
break;
18911872
case CollectionBits.ELEMENT_LONG:
1892-
b.beginMakeConstantLongList();
1893-
emitPythonConstant(constantCollection.collection, b);
1894-
b.endMakeConstantLongList();
1873+
b.emitMakeConstantLongList((long[]) constantCollection.collection);
18951874
break;
18961875
case CollectionBits.ELEMENT_BOOLEAN:
1897-
b.beginMakeConstantBooleanList();
1898-
emitPythonConstant(constantCollection.collection, b);
1899-
b.endMakeConstantBooleanList();
1876+
b.emitMakeConstantBooleanList((boolean[]) constantCollection.collection);
19001877
break;
19011878
case CollectionBits.ELEMENT_DOUBLE:
1902-
b.beginMakeConstantDoubleList();
1903-
emitPythonConstant(constantCollection.collection, b);
1904-
b.endMakeConstantDoubleList();
1879+
b.emitMakeConstantDoubleList((double[]) constantCollection.collection);
19051880
break;
19061881
case CollectionBits.ELEMENT_OBJECT:
1907-
b.beginMakeConstantObjectList();
1908-
emitPythonConstant(constantCollection.collection, b);
1909-
b.endMakeConstantObjectList();
1882+
b.emitMakeConstantObjectList((Object[]) constantCollection.collection);
19101883
break;
19111884
default:
19121885
throw CompilerDirectives.shouldNotReachHere();
19131886
}
19141887
}
19151888

19161889
private void emitConstantTuple(ConstantCollection constantCollection) {
1890+
addConstant(constantCollection.collection);
19171891
switch (constantCollection.elementType) {
19181892
case CollectionBits.ELEMENT_INT:
1919-
b.beginMakeConstantIntTuple();
1920-
emitPythonConstant(constantCollection.collection, b);
1921-
b.endMakeConstantIntTuple();
1893+
b.emitMakeConstantIntTuple((int[]) constantCollection.collection);
19221894
break;
19231895
case CollectionBits.ELEMENT_LONG:
1924-
b.beginMakeConstantLongTuple();
1925-
emitPythonConstant(constantCollection.collection, b);
1926-
b.endMakeConstantLongTuple();
1896+
b.emitMakeConstantLongTuple((long[]) constantCollection.collection);
19271897
break;
19281898
case CollectionBits.ELEMENT_BOOLEAN:
1929-
b.beginMakeConstantBooleanTuple();
1930-
emitPythonConstant(constantCollection.collection, b);
1931-
b.endMakeConstantBooleanTuple();
1899+
b.emitMakeConstantBooleanTuple((boolean[]) constantCollection.collection);
19321900
break;
19331901
case CollectionBits.ELEMENT_DOUBLE:
1934-
b.beginMakeConstantDoubleTuple();
1935-
emitPythonConstant(constantCollection.collection, b);
1936-
b.endMakeConstantDoubleTuple();
1902+
b.emitMakeConstantDoubleTuple((double[]) constantCollection.collection);
19371903
break;
19381904
case CollectionBits.ELEMENT_OBJECT:
1939-
b.beginMakeConstantObjectTuple();
1940-
emitPythonConstant(constantCollection.collection, b);
1941-
b.endMakeConstantObjectTuple();
1905+
b.emitMakeConstantObjectTuple((Object[]) constantCollection.collection);
19421906
break;
19431907
default:
19441908
throw CompilerDirectives.shouldNotReachHere();
@@ -3148,12 +3112,19 @@ private void emitMakeFunction(SSTNode node, String name, ArgumentsTy args, List<
31483112
BytecodeDSLCompilerResult compilerResult = compileNode(node);
31493113
BytecodeDSLCodeUnit codeUnit = compilerResult.codeUnit();
31503114

3151-
b.beginMakeFunction();
3152-
b.emitLoadConstant(toTruffleStringUncached(name));
3153-
3115+
TruffleString functionName = toTruffleStringUncached(name);
31543116
Scope targetScope = ctx.scopeEnvironment.lookupScope(node);
3155-
emitPythonConstant(toTruffleStringUncached(ctx.getQualifiedName(targetScope)), b);
3156-
emitPythonConstant(codeUnit, b);
3117+
TruffleString qualifiedName = toTruffleStringUncached(ctx.getQualifiedName(targetScope));
3118+
TruffleString doc = null;
3119+
if (codeUnit.constants.length > 0 && codeUnit.constants[0] != null && codeUnit.constants[0] instanceof TruffleString docString) {
3120+
doc = docString;
3121+
}
3122+
3123+
// Register these in the Python constants list.
3124+
addConstant(qualifiedName);
3125+
addConstant(codeUnit);
3126+
3127+
b.beginMakeFunction(functionName, qualifiedName, doc, codeUnit);
31573128

31583129
if (args == null || len(args.defaults) == 0) {
31593130
b.emitLoadConstant(PythonUtils.EMPTY_OBJECT_ARRAY);
@@ -3181,17 +3152,16 @@ private void emitMakeFunction(SSTNode node, String name, ArgumentsTy args, List<
31813152
} else {
31823153
ArgTy[] kwOnlyArgs = args.kwOnlyArgs;
31833154

3155+
List<TruffleString> keys = new ArrayList<>();
31843156
b.beginMakeKeywords();
3185-
int numKeywords = 0;
31863157
for (int i = 0; i < args.kwDefaults.length; i++) {
31873158
// Only emit keywords with default values.
31883159
if (args.kwDefaults[i] != null) {
3189-
b.emitLoadConstant(toTruffleStringUncached(mangle(kwOnlyArgs[i].arg)));
3160+
keys.add(toTruffleStringUncached(mangle(kwOnlyArgs[i].arg)));
31903161
args.kwDefaults[i].accept(this);
3191-
numKeywords++;
31923162
}
31933163
}
3194-
b.endMakeKeywords(numKeywords);
3164+
b.endMakeKeywords(keys.toArray(new TruffleString[0]));
31953165
}
31963166

31973167
if (codeUnit.freevars.length == 0) {
@@ -3222,14 +3192,6 @@ private void emitMakeFunction(SSTNode node, String name, ArgumentsTy args, List<
32223192
b.emitLoadConstant(null);
32233193
}
32243194

3225-
// __doc__
3226-
Object[] functionConstants = codeUnit.constants;
3227-
if (functionConstants.length > 0 && functionConstants[0] != null && functionConstants[0] instanceof TruffleString) {
3228-
b.emitLoadConstant(functionConstants[0]);
3229-
} else {
3230-
b.emitLoadConstant(null);
3231-
}
3232-
32333195
b.endMakeFunction();
32343196
}
32353197

@@ -3310,13 +3272,7 @@ public Void visit(StmtTy.Import node) {
33103272
: name.name;
33113273

33123274
beginStoreLocal(resName, b);
3313-
3314-
b.beginImport();
3315-
b.emitLoadConstant(toTruffleStringUncached(name.name));
3316-
b.emitLoadConstant(PythonUtils.EMPTY_TRUFFLESTRING_ARRAY);
3317-
b.emitLoadConstant(0);
3318-
b.endImport();
3319-
3275+
b.emitImport(toTruffleStringUncached(name.name), PythonUtils.EMPTY_TRUFFLESTRING_ARRAY, 0);
33203276
endStoreLocal(resName, b);
33213277
} else {
33223278
// import a.b.c as x
@@ -3329,18 +3285,13 @@ public Void visit(StmtTy.Import node) {
33293285

33303286
for (int i = parts.length - 1; i >= 0; i--) {
33313287
if (i != 0) {
3332-
b.beginImportFrom();
3288+
b.beginImportFrom(toTruffleStringUncached(parts[i]));
33333289
} else {
3334-
b.beginImport();
3335-
b.emitLoadConstant(toTruffleStringUncached(name.name));
3336-
b.emitLoadConstant(PythonUtils.EMPTY_TRUFFLESTRING_ARRAY);
3337-
b.emitLoadConstant(0);
3338-
b.endImport();
3290+
b.emitImport(toTruffleStringUncached(name.name), PythonUtils.EMPTY_TRUFFLESTRING_ARRAY, 0);
33393291
}
33403292
}
33413293

33423294
for (int i = 1; i < parts.length; i++) {
3343-
b.emitLoadConstant(toTruffleStringUncached(parts[i]));
33443295
b.endImportFrom();
33453296
}
33463297

@@ -3363,10 +3314,7 @@ public Void visit(StmtTy.ImportFrom node) {
33633314
TruffleString tsModuleName = toTruffleStringUncached(node.module == null ? "" : node.module);
33643315

33653316
if (node.names[0].name.equals("*")) {
3366-
b.beginImportStar();
3367-
b.emitLoadConstant(tsModuleName);
3368-
b.emitLoadConstant(node.level);
3369-
b.endImportStar();
3317+
b.emitImportStar(tsModuleName, node.level);
33703318
} else {
33713319
b.beginBlock();
33723320

@@ -3378,11 +3326,7 @@ public Void visit(StmtTy.ImportFrom node) {
33783326
}
33793327

33803328
b.beginStoreLocal(module);
3381-
b.beginImport();
3382-
b.emitLoadConstant(tsModuleName);
3383-
b.emitLoadConstant(fromList);
3384-
b.emitLoadConstant(node.level);
3385-
b.endImport();
3329+
b.emitImport(tsModuleName, fromList, node.level);
33863330
b.endStoreLocal();
33873331

33883332
TruffleString[] importedNames = new TruffleString[node.names.length];
@@ -3391,11 +3335,10 @@ public Void visit(StmtTy.ImportFrom node) {
33913335
String asName = alias.asName == null ? alias.name : alias.asName;
33923336
beginStoreLocal(asName, b);
33933337

3394-
b.beginImportFrom();
3395-
b.emitLoadLocal(module);
33963338
TruffleString name = toTruffleStringUncached(alias.name);
33973339
importedNames[i] = name;
3398-
b.emitLoadConstant(name);
3340+
b.beginImportFrom(name);
3341+
b.emitLoadLocal(module);
33993342
b.endImportFrom();
34003343

34013344
endStoreLocal(asName, b);

0 commit comments

Comments
 (0)