Skip to content

Commit 3ec245e

Browse files
committed
Fix argcells in LocalsStorage
1 parent fbb5073 commit 3ec245e

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public final class CodeUnit {
8282
public final TruffleString[] cellvars;
8383
public final TruffleString[] freevars;
8484
public final int[] cell2arg;
85+
public final int[] arg2cell;
8586

8687
public final Object[] constants;
8788
public final long[] primitiveConstants;
@@ -109,6 +110,7 @@ public CodeUnit(TruffleString name, TruffleString qualname,
109110
int[] exceptionHandlerRanges, int conditionProfileCount,
110111
int startOffset, int startLine,
111112
byte[] outputCanQuicken, byte[] variableShouldUnbox, int[][] generalizeInputsMap, int[][] generalizeVarsMap) {
113+
int[] arg2cell;
112114
this.name = name;
113115
this.qualname = qualname != null ? qualname : name;
114116
this.argCount = argCount;
@@ -123,6 +125,17 @@ public CodeUnit(TruffleString name, TruffleString qualname,
123125
this.cellvars = cellvars;
124126
this.freevars = freevars;
125127
this.cell2arg = cell2arg;
128+
arg2cell = null;
129+
if (cell2arg != null) {
130+
arg2cell = new int[getTotalArgCount()];
131+
Arrays.fill(arg2cell, -1);
132+
for (int i = 0; i < cell2arg.length; i++) {
133+
if (cell2arg[i] >= 0) {
134+
arg2cell[cell2arg[i]] = i;
135+
}
136+
}
137+
}
138+
this.arg2cell = arg2cell;
126139
this.constants = constants;
127140
this.primitiveConstants = primitiveConstants;
128141
this.exceptionHandlerRanges = exceptionHandlerRanges;
@@ -218,10 +231,21 @@ public boolean isGeneratorOrCoroutine() {
218231
return (flags & (PCode.CO_GENERATOR | PCode.CO_COROUTINE | PCode.CO_ASYNC_GENERATOR | PCode.CO_ITERABLE_COROUTINE)) != 0;
219232
}
220233

221-
public int getTotalArgCount() {
234+
public int getRegularArgCount() {
222235
return argCount + positionalOnlyArgCount + kwOnlyArgCount;
223236
}
224237

238+
public int getTotalArgCount() {
239+
int count = getRegularArgCount();
240+
if (takesVarArgs()) {
241+
count++;
242+
}
243+
if (takesVarKeywordArgs()) {
244+
count++;
245+
}
246+
return count;
247+
}
248+
225249
@SuppressWarnings("fallthrough")
226250
@Override
227251
public String toString() {

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,15 @@ private static FrameDescriptor makeFrameDescriptor(CodeUnit co) {
496496
newBuilder.info(new FrameInfo());
497497
// locals
498498
for (int i = 0; i < co.varnames.length; i++) {
499-
newBuilder.addSlot(FrameSlotKind.Illegal, co.varnames[i], null);
499+
TruffleString varname = co.varnames[i];
500+
if (co.arg2cell != null && i < co.arg2cell.length && co.arg2cell[i] >= 0) {
501+
/*
502+
* If an argument is a cell, its slot gets superseded by the cell's slot below. We
503+
* need to hide it from LocalsStorage and other introspection.
504+
*/
505+
varname = null;
506+
}
507+
newBuilder.addSlot(FrameSlotKind.Illegal, varname, null);
500508
}
501509
// cells
502510
for (int i = 0; i < co.cellvars.length; i++) {
@@ -776,7 +784,7 @@ private void copyArgs(Object[] args, Frame localFrame) {
776784
copyArgsFirstTime(args, localFrame);
777785
return;
778786
}
779-
int argCount = co.getTotalArgCount();
787+
int argCount = co.getRegularArgCount();
780788
for (int i = 0; i < argCount; i++) {
781789
Object arg = args[i + PArguments.USER_ARGUMENTS_OFFSET];
782790
if (variableTypes[i] == QuickeningTypes.OBJECT) {
@@ -799,7 +807,7 @@ private void copyArgs(Object[] args, Frame localFrame) {
799807
private void copyArgsFirstTime(Object[] args, Frame localFrame) {
800808
CompilerAsserts.neverPartOfCompilation();
801809
variableTypes = new byte[varnames.length];
802-
int argCount = co.getTotalArgCount();
810+
int argCount = co.getRegularArgCount();
803811
for (int i = 0; i < argCount; i++) {
804812
Object arg = args[i + PArguments.USER_ARGUMENTS_OFFSET];
805813
if ((variableShouldUnbox[i] & QuickeningTypes.INT) != 0 && arg instanceof Integer) {
@@ -831,7 +839,7 @@ public void createGeneratorFrame(Object[] arguments) {
831839

832840
private void copyArgsAndCells(Frame localFrame, Object[] arguments) {
833841
copyArgs(arguments, localFrame);
834-
int varIdx = co.getTotalArgCount();
842+
int varIdx = co.getRegularArgCount();
835843
if (co.takesVarArgs()) {
836844
localFrame.setObject(varIdx++, factory.createTuple(PArguments.getVariableArguments(arguments)));
837845
}

0 commit comments

Comments
 (0)