Skip to content

Commit 803ee6d

Browse files
cosminbascatomasstupka
authored andcommitted
PCode: add support for setting the flags from CodeUnit internal flags
1 parent bc2becd commit 803ee6d

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@
9797
public final class PCode extends PythonBuiltinObject {
9898
public static final long FLAG_VAR_ARGS = 0x4;
9999
public static final long FLAG_VAR_KW_ARGS = 0x8;
100-
static final long FLAG_LAMBDA = 0x10; // CO_NESTED on CPython, not needed
101-
static final long FLAG_GENERATOR = 0x20;
102-
static final long FLAG_MODULE = 0x40; // CO_NOFREE on CPython, we use it on modules, it's
103-
// redundant anyway
100+
public static final long FLAG_LAMBDA = 0x10; // CO_NESTED on CPython, not needed
101+
public static final long FLAG_GENERATOR = 0x20;
102+
public static final long FLAG_MODULE = 0x40; // CO_NOFREE on CPython, we use it on modules, it's
103+
// redundant anyway
104104

105105
// callTargetSupplier may be null, in which case callTarget and signature will be
106106
// set. Otherwise, these are lazily created from the supplier.
@@ -383,6 +383,14 @@ private static int extractFlags(RootNode rootNode) {
383383
if (funcRootNode instanceof ModuleRootNode) {
384384
// Not on CPython
385385
flags |= FLAG_MODULE;
386+
}
387+
if (funcRootNode instanceof PBytecodeRootNode) {
388+
CodeUnit codeUnit = ((PBytecodeRootNode) funcRootNode).getCodeUnit();
389+
flags = getFlags(flags, codeUnit);
390+
}
391+
if (funcRootNode instanceof PBytecodeGeneratorFunctionRootNode) {
392+
CodeUnit codeUnit = ((PBytecodeGeneratorFunctionRootNode) funcRootNode).getBytecodeRootNode().getCodeUnit();
393+
flags = getFlags(flags, codeUnit);
386394
} else {
387395
// 0x20 - generator
388396
if (funcRootNode instanceof GeneratorFunctionRootNode) {
@@ -405,6 +413,14 @@ private static int extractFlags(RootNode rootNode) {
405413
return flags;
406414
}
407415

416+
private static int getFlags(int flags, CodeUnit codeUnit) {
417+
flags |= codeUnit.isGenerator() ? FLAG_GENERATOR : 0;
418+
flags |= codeUnit.takesVarArgs() ? FLAG_VAR_ARGS : 0;
419+
flags |= codeUnit.takesVarKeywordArgs() ? FLAG_VAR_KW_ARGS : 0;
420+
flags |= codeUnit.isLambda() ? FLAG_LAMBDA : 0;
421+
return flags;
422+
}
423+
408424
RootNode getRootNode() {
409425
return getRootCallTarget().getRootNode();
410426
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.oracle.graal.python.builtins.objects.bytes.BytesUtils;
5050
import com.oracle.graal.python.builtins.objects.str.PString;
5151
import com.oracle.graal.python.compiler.OpCodes.CollectionBits;
52+
import com.oracle.graal.python.nodes.BuiltinNames;
5253
import com.oracle.graal.python.util.PythonUtils;
5354

5455
/**
@@ -94,6 +95,8 @@ public final class CodeUnit {
9495

9596
public final int startOffset;
9697

98+
public final boolean lambda;
99+
97100
public CodeUnit(String name, String qualname,
98101
int argCount, int kwOnlyArgCount, int positionalOnlyArgCount, int stacksize,
99102
byte[] code, byte[] linetable, int flags,
@@ -118,6 +121,7 @@ public CodeUnit(String name, String qualname,
118121
this.primitiveConstants = primitiveConstants;
119122
this.exceptionHandlerRanges = exceptionHandlerRanges;
120123
this.startOffset = startOffset;
124+
this.lambda = name.equals(BuiltinNames.LAMBDA_NAME);
121125
}
122126

123127
OpCodes codeForBC(int bc) {
@@ -186,6 +190,10 @@ public boolean isGeneratorOrCoroutine() {
186190
return (flags & (IS_GENERATOR | IS_COROUTINE)) != 0;
187191
}
188192

193+
public boolean isLambda() {
194+
return lambda;
195+
}
196+
189197
@SuppressWarnings("fallthrough")
190198
@Override
191199
public String toString() {

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int makeFunction(Object globals, int initialStackTop, Frame localFrame, int flag
9393
PCode codeObj = cachedCode;
9494
if (codeObj == null) {
9595
// Multi-context mode
96-
codeObj = createCode(factory, code, callTarget, signature);
96+
codeObj = factory.createCode(callTarget, signature, code);
9797
}
9898

9999
PCell[] closure = null;
@@ -140,11 +140,6 @@ int makeFunction(Object globals, int initialStackTop, Frame localFrame, int flag
140140
return stackTop;
141141
}
142142

143-
private static PCode createCode(PythonObjectFactory factory, CodeUnit code, RootCallTarget callTarget, Signature signature) {
144-
return factory.createCode(callTarget, signature, code.varnames.length, code.stacksize, code.flags, code.constants, code.names,
145-
code.varnames, code.freevars, code.cellvars, null, code.name, code.startOffset, code.srcOffsetTable);
146-
}
147-
148143
public static MakeFunctionNode create(PythonLanguage language, CodeUnit code, Source source) {
149144
RootCallTarget callTarget;
150145
PBytecodeRootNode bytecodeRootNode = new PBytecodeRootNode(language, code, source);
@@ -156,7 +151,7 @@ public static MakeFunctionNode create(PythonLanguage language, CodeUnit code, So
156151
}
157152
PCode cachedCode = null;
158153
if (language.isSingleContext()) {
159-
cachedCode = createCode(PythonObjectFactory.getUncached(), code, callTarget, bytecodeRootNode.getSignature());
154+
cachedCode = PythonObjectFactory.getUncached().createCode(callTarget, bytecodeRootNode.getSignature(), code);
160155
}
161156
String doc = null;
162157
if (code.constants.length > 0 && code.constants[0] instanceof String) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/object/PythonObjectFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
191191
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetMroStorageNode;
192192
import com.oracle.graal.python.builtins.objects.zipimporter.PZipImporter;
193+
import com.oracle.graal.python.compiler.CodeUnit;
193194
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
194195
import com.oracle.graal.python.nodes.literal.ListLiteralNode;
195196
import com.oracle.graal.python.parser.ExecutionCellSlots;
@@ -1091,6 +1092,11 @@ public final PCode createCode(RootCallTarget ct, int flags, int firstlineno, byt
10911092
return trace(new PCode(PythonBuiltinClassType.PCode, PythonBuiltinClassType.PCode.getInstanceShape(getLanguage()), ct, flags, firstlineno, lnotab, filename));
10921093
}
10931094

1095+
public final PCode createCode(RootCallTarget callTarget, Signature signature, CodeUnit codeUnit) {
1096+
return createCode(callTarget, signature, codeUnit.varnames.length, codeUnit.stacksize, -1, codeUnit.constants, codeUnit.names,
1097+
codeUnit.varnames, codeUnit.freevars, codeUnit.cellvars, null, codeUnit.name, codeUnit.startOffset, codeUnit.srcOffsetTable);
1098+
}
1099+
10941100
public final PCode createCode(RootCallTarget callTarget, Signature signature, int nlocals, int stacksize, int flags, Object[] constants, Object[] names, Object[] varnames,
10951101
Object[] freevars, Object[] cellvars, String filename, String name, int firstlineno, byte[] lnotab) {
10961102
return trace(new PCode(PythonBuiltinClassType.PCode, getShape(PythonBuiltinClassType.PCode), callTarget, signature, nlocals, stacksize, flags, constants, names, varnames, freevars, cellvars,

0 commit comments

Comments
 (0)