Skip to content

Commit d3e38fb

Browse files
committed
Re-structure 'DSL enabled' checks, make constants PE-const, start cleaning up Truffle DSL warnings
1 parent aadd7d6 commit d3e38fb

File tree

7 files changed

+493
-356
lines changed

7 files changed

+493
-356
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/GraalPythonModuleBuiltins.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,10 @@ public Object doIt(VirtualFrame frame, PFunction func,
520520
@TruffleBoundary
521521
public synchronized PFunction convertToBuiltin(PFunction func) {
522522
RootNode rootNode = CodeNodes.GetCodeRootNode.executeUncached(func.getCode());
523-
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && rootNode instanceof PBytecodeDSLRootNode r) {
524-
r.setPythonInternal(true);
523+
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
524+
if (rootNode instanceof PBytecodeDSLRootNode r) {
525+
r.setPythonInternal(true);
526+
}
525527
} else if (rootNode instanceof PBytecodeRootNode r) {
526528
r.setPythonInternal(true);
527529
}
@@ -538,8 +540,10 @@ public Object doIt(PFunction func,
538540
@Bind("this") Node inliningTarget,
539541
@Cached CodeNodes.GetCodeRootNode getRootNode) {
540542
RootNode rootNode = getRootNode.execute(inliningTarget, func.getCode());
541-
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && rootNode instanceof PBytecodeDSLRootNode r) {
542-
r.setPythonInternal(true);
543+
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
544+
if (rootNode instanceof PBytecodeDSLRootNode r) {
545+
r.setPythonInternal(true);
546+
}
543547
} else if (rootNode instanceof PBytecodeRootNode r) {
544548
r.setPythonInternal(true);
545549
}

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ private static int extractStackSize(RootNode rootNode) {
310310
/**
311311
* NB: This fallback case includes PBytecodeDSLRootNode. The Bytecode DSL stack does not
312312
* mirror a CPython stack (it's an operand stack for its own instruction set), so the frame
313-
* size is our best guess.
313+
* size is our best estimate.
314314
*/
315315
return funcRootNode.getFrameDescriptor().getNumberOfSlots();
316316
}
@@ -327,14 +327,16 @@ private static TruffleString[] extractVarnames(RootNode node) {
327327
@TruffleBoundary
328328
private static Object[] extractConstants(RootNode node) {
329329
RootNode rootNode = rootNodeForExtraction(node);
330-
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && rootNode instanceof PBytecodeDSLRootNode bytecodeDSLRootNode) {
331-
BytecodeDSLCodeUnit co = bytecodeDSLRootNode.getCodeUnit();
332-
List<Object> constants = new ArrayList<>();
333-
for (int i = 0; i < co.constants.length; i++) {
334-
Object constant = convertConstantToPythonSpace(rootNode, co.constants[i]);
335-
constants.add(constant);
330+
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
331+
if (rootNode instanceof PBytecodeDSLRootNode bytecodeDSLRootNode) {
332+
BytecodeDSLCodeUnit co = bytecodeDSLRootNode.getCodeUnit();
333+
List<Object> constants = new ArrayList<>();
334+
for (int i = 0; i < co.constants.length; i++) {
335+
Object constant = convertConstantToPythonSpace(rootNode, co.constants[i]);
336+
constants.add(constant);
337+
}
338+
return constants.toArray(new Object[0]);
336339
}
337-
return constants.toArray(new Object[0]);
338340
} else if (rootNode instanceof PBytecodeRootNode bytecodeRootNode) {
339341
BytecodeCodeUnit co = bytecodeRootNode.getCodeUnit();
340342
Set<Object> bytecodeConstants = new HashSet<>();
@@ -413,8 +415,10 @@ private static int extractFlags(RootNode node) {
413415

414416
private static CodeUnit getCodeUnit(RootNode node) {
415417
RootNode rootNode = rootNodeForExtraction(node);
416-
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && rootNode instanceof PBytecodeDSLRootNode bytecodeDSLRootNode) {
417-
return bytecodeDSLRootNode.getCodeUnit();
418+
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
419+
if (rootNode instanceof PBytecodeDSLRootNode bytecodeDSLRootNode) {
420+
return bytecodeDSLRootNode.getCodeUnit();
421+
}
418422
} else if (rootNode instanceof PBytecodeRootNode bytecodeRootNode) {
419423
return bytecodeRootNode.getCodeUnit();
420424
}

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

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,16 +1664,20 @@ public Void visit(ExprTy.Constant node) {
16641664
public Void visit(ExprTy.Dict node) {
16651665
beginNode(node);
16661666

1667-
b.beginMakeDict();
1668-
for (int i = 0; i < node.keys.length; i++) {
1669-
if (node.keys[i] == null) {
1670-
b.emitLoadConstant(PNone.NO_VALUE);
1671-
} else {
1672-
node.keys[i].accept(this);
1667+
if (len(node.keys) == 0) {
1668+
b.emitMakeEmptyDict();
1669+
} else {
1670+
b.beginMakeDict();
1671+
for (int i = 0; i < node.keys.length; i++) {
1672+
if (node.keys[i] == null) {
1673+
b.emitLoadConstant(PNone.NO_VALUE);
1674+
} else {
1675+
node.keys[i].accept(this);
1676+
}
1677+
node.values[i].accept(this);
16731678
}
1674-
node.values[i].accept(this);
1679+
b.endMakeDict();
16751680
}
1676-
b.endMakeDict();
16771681

16781682
endNode(node);
16791683
return null;
@@ -1940,8 +1944,8 @@ private void emitUnstar(ExprTy[] args, BytecodeLocal receiver) {
19401944
* MakeVariadic(a, b),
19411945
* UnpackStarred(c),
19421946
* MakeVariadic(d, e),
1943-
* MakeVariadic(f),
1944-
* UnpackStarred(g)
1947+
* UnpackStarred(f),
1948+
* MakeVariadic(g)
19451949
* )
19461950
* @formatter:on
19471951
*/
@@ -1993,9 +1997,13 @@ private void emitUnstar(ExprTy[] args, BytecodeLocal receiver) {
19931997
public Void visit(ExprTy.Set node) {
19941998
beginNode(node);
19951999

1996-
b.beginMakeSet();
1997-
emitUnstar(node.elements);
1998-
b.endMakeSet();
2000+
if (len(node.elements) == 0) {
2001+
b.emitMakeEmptySet();
2002+
} else {
2003+
b.beginMakeSet();
2004+
emitUnstar(node.elements);
2005+
b.endMakeSet();
2006+
}
19992007

20002008
endNode(node);
20012009
return null;
@@ -3115,7 +3123,18 @@ private void emitMakeFunction(SSTNode node, String name, ArgumentsTy args, List<
31153123
b.endMakeVariadic();
31163124
}
31173125

3118-
if (args == null || len(args.kwDefaults) == 0) {
3126+
boolean hasKeywords = false;
3127+
if (args != null && len(args.kwDefaults) != 0) {
3128+
// We only emit keywords with default values. Check if any exist.
3129+
for (int i = 0; i < args.kwDefaults.length; i++) {
3130+
if (args.kwDefaults[i] != null) {
3131+
hasKeywords = true;
3132+
break;
3133+
}
3134+
}
3135+
}
3136+
3137+
if (!hasKeywords) {
31193138
b.emitLoadConstant(PKeyword.EMPTY_KEYWORDS);
31203139
} else {
31213140
ArgTy[] kwOnlyArgs = args.kwOnlyArgs;
@@ -3592,12 +3611,8 @@ private void doVisitPattern(PatternTy.MatchOr node) {
35923611
emitPatternNotImplemented("OR");
35933612
}
35943613

3595-
private static int lengthOrZero(Object[] p) {
3596-
return p == null ? 0 : p.length;
3597-
}
3598-
35993614
private void patternHelperSequenceUnpack(PatternTy[] patterns, PatternContext pc) {
3600-
int n = lengthOrZero(patterns);
3615+
int n = len(patterns);
36013616

36023617
b.beginBlock();
36033618
// We need to remember the unpacked array, since subject will be overwritten in
@@ -3621,7 +3636,7 @@ private void patternHelperSequenceUnpack(PatternTy[] patterns, PatternContext pc
36213636
}
36223637

36233638
private void patternUnpackHelper(PatternTy[] patterns, PatternContext pc) {
3624-
int n = lengthOrZero(patterns);
3639+
int n = len(patterns);
36253640

36263641
boolean seenStar = false;
36273642
for (int i = 0; i < n; i++) {
@@ -3659,7 +3674,7 @@ private void patternUnpackHelper(PatternTy[] patterns, PatternContext pc) {
36593674
* etc.
36603675
*/
36613676
private void patternHelperSequenceSubscr(PatternTy[] patterns, int star, PatternContext pc) {
3662-
int n = lengthOrZero(patterns);
3677+
int n = len(patterns);
36633678

36643679
b.beginBlock();
36653680
// We need to remember the sequence, since subject will be overwritten in recursive
@@ -3707,7 +3722,7 @@ private void patternHelperSequenceSubscr(PatternTy[] patterns, int star, Pattern
37073722
}
37083723

37093724
private void doVisitPattern(PatternTy.MatchSequence node, PatternContext pc) {
3710-
int size = lengthOrZero(node.patterns);
3725+
int size = len(node.patterns);
37113726
int star = -1;
37123727
boolean onlyWildcard = true;
37133728
boolean starWildcard = false;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.oracle.truffle.api.dsl.Cached;
5656
import com.oracle.truffle.api.dsl.GenerateInline;
5757
import com.oracle.truffle.api.dsl.GenerateUncached;
58+
import com.oracle.truffle.api.dsl.NeverDefault;
5859
import com.oracle.truffle.api.dsl.Specialization;
5960
import com.oracle.truffle.api.frame.Frame;
6061
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -79,6 +80,7 @@ void print(VirtualFrame frame, Object object,
7980
callNode.execute(frame, displayhook, object);
8081
}
8182

83+
@NeverDefault
8284
public static PrintExprNode create() {
8385
return PrintExprNodeGen.create();
8486
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.oracle.truffle.api.dsl.Fallback;
5252
import com.oracle.truffle.api.dsl.GenerateInline;
5353
import com.oracle.truffle.api.dsl.ImportStatic;
54+
import com.oracle.truffle.api.dsl.NeverDefault;
5455
import com.oracle.truffle.api.dsl.Specialization;
5556
import com.oracle.truffle.api.frame.VirtualFrame;
5657
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -243,6 +244,7 @@ private static PException raiseNoException(PRaiseNode raise) {
243244
throw raise.raise(TypeError, ErrorMessages.EXCEPTIONS_MUST_DERIVE_FROM_BASE_EX);
244245
}
245246

247+
@NeverDefault
246248
public static RaiseNode create() {
247249
return RaiseNodeGen.create();
248250
}

0 commit comments

Comments
 (0)