Skip to content

Commit 1d34ed9

Browse files
committed
PCode: handle module root node for pro extraction + codestring (source code)
- fix getFreeVars in PClosureRootNode - fix extractCode - create calltarget from source
1 parent 9104425 commit 1d34ed9

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
import com.oracle.truffle.api.CompilerDirectives;
172172
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
173173
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
174+
import com.oracle.truffle.api.RootCallTarget;
174175
import com.oracle.truffle.api.Truffle;
175176
import com.oracle.truffle.api.debug.Debugger;
176177
import com.oracle.truffle.api.dsl.Cached;
@@ -193,6 +194,7 @@
193194
import com.oracle.truffle.api.profiles.BranchProfile;
194195
import com.oracle.truffle.api.profiles.ConditionProfile;
195196
import com.oracle.truffle.api.source.Source;
197+
import org.graalvm.polyglot.io.ByteSequence;
196198

197199
@CoreFunctions(defineModule = "builtins")
198200
public final class BuiltinFunctions extends PythonBuiltins {
@@ -681,7 +683,18 @@ Object execCustomGlobalsGlobalLocals(Object source, PDict globals, @SuppressWarn
681683
// fall back (like their CPython counterparts) to writing to the globals. We only need
682684
// to ensure that the `locals()` call still gives us the globals dict
683685
PArguments.setPFrame(args, factory().createPFrame(globals));
684-
return indirectCallNode.call(code.getRootCallTarget(), args);
686+
RootCallTarget rootCallTarget = code.getRootCallTarget();
687+
if (rootCallTarget == null) {
688+
Source src = Source.newBuilder("python", new String(code.getCodestring()), code.getName()).build();
689+
Node root = getCore().getParser().parse(ParserMode.File, getCore(), src, null);
690+
if (root instanceof RootNode) {
691+
rootCallTarget = Truffle.getRuntime().createCallTarget((RootNode) root);
692+
}
693+
}
694+
if (rootCallTarget == null) {
695+
throw raise(ValueError, "cannot create the rootCallTarget from the code object: %p", code);
696+
}
697+
return indirectCallNode.call(rootCallTarget, args);
685698
}
686699

687700
@Specialization(guards = {"isMapping(locals)"})

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -45,6 +45,7 @@
4545
import java.util.List;
4646
import java.util.Set;
4747

48+
import com.oracle.graal.python.PythonLanguage;
4849
import com.oracle.graal.python.builtins.objects.function.Arity;
4950
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
5051
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
@@ -53,19 +54,25 @@
5354
import com.oracle.graal.python.nodes.argument.ReadKeywordNode;
5455
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
5556
import com.oracle.graal.python.nodes.argument.ReadVarKeywordsNode;
57+
import com.oracle.graal.python.nodes.control.TopLevelExceptionHandler;
5658
import com.oracle.graal.python.nodes.frame.FrameSlotIDs;
5759
import com.oracle.graal.python.nodes.frame.WriteIdentifierNode;
5860
import com.oracle.graal.python.nodes.function.FunctionRootNode;
5961
import com.oracle.graal.python.nodes.generator.GeneratorFunctionRootNode;
62+
import com.oracle.graal.python.runtime.PythonContext;
6063
import com.oracle.graal.python.runtime.PythonCore;
61-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
64+
import com.oracle.graal.python.runtime.PythonParser;
65+
import com.oracle.graal.python.runtime.exception.PException;
6266
import com.oracle.truffle.api.CompilerDirectives;
67+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6368
import com.oracle.truffle.api.RootCallTarget;
6469
import com.oracle.truffle.api.Truffle;
6570
import com.oracle.truffle.api.nodes.Node;
6671
import com.oracle.truffle.api.nodes.NodeUtil;
6772
import com.oracle.truffle.api.nodes.RootNode;
73+
import com.oracle.truffle.api.source.Source;
6874
import com.oracle.truffle.api.source.SourceSection;
75+
import org.graalvm.polyglot.io.ByteSequence;
6976

7077
public final class PCode extends PythonBuiltinObject {
7178
private final long FLAG_POS_GENERATOR = 5;
@@ -158,6 +165,8 @@ private static String[] extractFreeVars(RootNode rootNode) {
158165
return ((FunctionRootNode) rootNode).getFreeVars();
159166
} else if (rootNode instanceof GeneratorFunctionRootNode) {
160167
return ((GeneratorFunctionRootNode) rootNode).getFreeVars();
168+
} else if (rootNode instanceof ModuleRootNode) {
169+
return ((ModuleRootNode) rootNode).getFreeVars();
161170
} else {
162171
return null;
163172
}
@@ -168,6 +177,8 @@ private static String[] extractCellVars(RootNode rootNode) {
168177
return ((FunctionRootNode) rootNode).getCellVars();
169178
} else if (rootNode instanceof GeneratorFunctionRootNode) {
170179
return ((GeneratorFunctionRootNode) rootNode).getCellVars();
180+
} else if (rootNode instanceof ModuleRootNode) {
181+
return new String[0];
171182
} else {
172183
return null;
173184
}
@@ -189,13 +200,16 @@ private static String extractFileName(RootNode rootNode) {
189200
private static int extractFirstLineno(RootNode rootNode) {
190201
RootNode funcRootNode = (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
191202
SourceSection sourceSection = funcRootNode.getSourceSection();
192-
return (sourceSection != null) ? sourceSection.getStartLine() : 1;
203+
if (sourceSection != null) {
204+
return sourceSection.getStartLine();
205+
}
206+
return 1;
193207
}
194208

195209
private static String extractName(RootNode rootNode) {
196210
String name;
197211
if (rootNode instanceof ModuleRootNode) {
198-
name = "<module>";
212+
name = rootNode.getName();
199213
} else if (rootNode instanceof FunctionRootNode) {
200214
name = ((FunctionRootNode) rootNode).getFunctionName();
201215
} else {
@@ -299,6 +313,18 @@ private void extractArgStats() {
299313
this.nlocals = varnamesSet.size();
300314
}
301315

316+
@TruffleBoundary
317+
public void extractCodeString(RootNode rootNode) {
318+
RootNode funcRootNode = rootNode;
319+
if (rootNode instanceof GeneratorFunctionRootNode) {
320+
funcRootNode = ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode();
321+
}
322+
SourceSection sourceSection = funcRootNode.getSourceSection();
323+
if (sourceSection != null) {
324+
this.codestring = sourceSection.getCharacters().toString().getBytes();
325+
}
326+
}
327+
302328
public RootNode getRootNode() {
303329
return rootNode;
304330
}
@@ -389,6 +415,9 @@ public Object[] getVarnames() {
389415
}
390416

391417
public byte[] getCodestring() {
418+
if (codestring == null && hasRootNode()) {
419+
extractCodeString(getRootNode());
420+
}
392421
return codestring;
393422
}
394423

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PClosureRootNode.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ public boolean hasFreeVars() {
9090
}
9191

9292
public String[] getFreeVars() {
93-
String[] freeVars = new String[freeVarSlots.length];
94-
for (int i = 0; i < freeVarSlots.length; i++) {
95-
freeVars[i] = (String) freeVarSlots[i].getIdentifier();
93+
if (freeVarSlots != null) {
94+
String[] freeVars = new String[freeVarSlots.length];
95+
for (int i = 0; i < freeVarSlots.length; i++) {
96+
freeVars[i] = (String) freeVarSlots[i].getIdentifier();
97+
}
98+
return freeVars;
9699
}
97-
return freeVars;
100+
return null;
98101
}
99102
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/ExpressionNode.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -53,6 +53,8 @@
5353
import com.oracle.truffle.api.nodes.Node;
5454
import com.oracle.truffle.api.nodes.NodeCost;
5555
import com.oracle.truffle.api.nodes.UnexpectedResultException;
56+
import com.oracle.truffle.api.source.Source;
57+
import com.oracle.truffle.api.source.SourceSection;
5658

5759
/**
5860
* Base class for all expressions. Expressions always return a value.
@@ -176,7 +178,11 @@ public static final class ExpressionWithSideEffects extends ExpressionNode {
176178
private ExpressionWithSideEffects(ExpressionNode node, StatementNode[] sideEffects) {
177179
this.node = node;
178180
this.sideEffects = sideEffects;
179-
this.assignSourceSection(node.getSourceSection());
181+
SourceSection sourceSection = node.getSourceSection();
182+
if (sourceSection!=null) {
183+
Source source = sourceSection.getSource();
184+
this.assignSourceSection(source.createSection(0, source.getLength()));
185+
}
180186
}
181187

182188
@Override

0 commit comments

Comments
 (0)