Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/core/IronPython/Compiler/Ast/AstMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ internal static class AstMethods {
public static readonly MethodInfo ForLoopDispose = GetMethod((Action<KeyValuePair<IEnumerator, IDisposable>>)PythonOps.ForLoopDispose);
public static readonly MethodInfo GetClosureTupleFromContext = GetMethod((Func<CodeContext, MutableTuple>)PythonOps.GetClosureTupleFromContext);
public static readonly MethodInfo PushFrame = GetMethod((Func<CodeContext, FunctionCode, List<FunctionStack>>)PythonOps.PushFrame);
public static readonly MethodInfo PushFrameWithLineNumber = GetMethod((PushFrameWithLineNumberDelegate)PythonOps.PushFrame);
#if DEBUG
public static readonly MethodInfo PopFrame = GetMethod((Action<CodeContext, FunctionCode, List<FunctionStack>>)PythonOps.PopFrame);
#else
public static readonly MethodInfo PopFrame = GetMethod((Action<List<FunctionStack>>)PythonOps.PopFrame);
#endif
public static readonly MethodInfo PushGeneratorFrame = GetMethod((Action<CodeContext, PythonGenerator, FunctionCode>)PythonOps.PushGeneratorFrame);
public static readonly MethodInfo FormatString = GetMethod((Func<CodeContext, string, object, string>)PythonOps.FormatString);
Expand Down Expand Up @@ -108,5 +111,8 @@ internal static class AstMethods {
private static MethodInfo GetMethod(Delegate x) {
return x.Method;
}

// Func can't express a by-ref parameter
private delegate List<FunctionStack> PushFrameWithLineNumberDelegate(CodeContext context, FunctionCode function, ref int lineNo);
}
}
79 changes: 51 additions & 28 deletions src/core/IronPython/Compiler/Ast/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,12 @@ internal static MSAst.Expression RemoveDebugInfo(int prevStart, MSAst.Expression
/// <summary>
/// Creates a method frame for tracking purposes and enforces recursion
/// </summary>
internal static MSAst.Expression AddFrame(MSAst.Expression localContext, MSAst.Expression codeObject, MSAst.Expression body)
=> new FramedCodeExpression(localContext, codeObject, body, inGenerator: false);
/// <param name="publishLineNumber">
/// Whether compiled code publishes its current line through the frame. It requires <see cref="LineNumberExpression"/> to be
/// declared around the frame, which <see cref="ScopeStatement.WrapScopeStatements"/> does for code that can throw.
/// </param>
internal static MSAst.Expression AddFrame(MSAst.Expression localContext, MSAst.Expression codeObject, MSAst.Expression body, bool publishLineNumber = true)
=> new FramedCodeExpression(localContext, codeObject, body, inGenerator: false, publishLineNumber);

/// <summary>
/// Creates a method frame for the body of a generator, which hands the frame over to its <see cref="PythonGenerator"/>.
Expand All @@ -328,7 +332,7 @@ internal static MSAst.Expression AddFrame(MSAst.Expression localContext, MSAst.E
/// and removes it when the body finishes.
/// </remarks>
internal static MSAst.Expression AddGeneratorFrame(MSAst.Expression localContext, MSAst.Expression codeObject, MSAst.Expression body)
=> new FramedCodeExpression(localContext, codeObject, body, inGenerator: true);
=> new FramedCodeExpression(localContext, codeObject, body, inGenerator: true, publishLineNumber: false);

#if FEATURE_NET_ASYNC
/// <summary>
Expand All @@ -340,7 +344,7 @@ internal static MSAst.Expression AddGeneratorFrame(MSAst.Expression localContext
/// The <see cref="AsyncBodyFrame"/>, the observer of the body's driver, moves the frame along with the body.
/// </remarks>
internal static MSAst.Expression AddAsyncFrame(MSAst.Expression localContext, MSAst.Expression asyncFrame, MSAst.Expression codeObject, MSAst.Expression body)
=> new FramedCodeExpression(localContext, codeObject, body, inGenerator: false, asyncFrame);
=> new FramedCodeExpression(localContext, codeObject, body, inGenerator: false, publishLineNumber: false, asyncFrame);
#endif

/// <summary>
Expand All @@ -359,17 +363,30 @@ private class FramedCodeVisitor : ExpressionVisitor {
}
}

private sealed class FramedCodeExpression : MSAst.Expression {
/// <remarks>
/// Compiled code that can't suspend publishes its current line through its frame stack entry, as a pointer to
/// <see cref="LineNumberExpression"/>, which is a local of the compiled method. Interpreted code doesn't, since the
/// interpreter keeps its locals in the interpreted frame and passes a reference to a copy of the variable.
/// </remarks>
private sealed class FramedCodeExpression : MSAst.Expression, IInstructionProvider {
private readonly MSAst.Expression _localContext, _codeObject;
private readonly bool _inGenerator;
private readonly bool _inGenerator, _publishLineNumber;

// For an async body: the AsyncBodyFrame the body hands its frame over to.
private readonly MSAst.Expression? _asyncFrame;

public FramedCodeExpression(MSAst.Expression localContext, MSAst.Expression codeObject, MSAst.Expression body, bool inGenerator, MSAst.Expression? asyncFrame = null) {
// Where dynamic code isn't supported, LambdaExpression.Compile interprets the lambda, so its locals aren't on the stack.
#if NET
private static bool CanPublishLineNumber => System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeSupported;
#else
private static bool CanPublishLineNumber => true;
#endif

public FramedCodeExpression(MSAst.Expression localContext, MSAst.Expression codeObject, MSAst.Expression body, bool inGenerator, bool publishLineNumber, MSAst.Expression? asyncFrame = null) {
_localContext = localContext;
_codeObject = codeObject;
_inGenerator = inGenerator;
_publishLineNumber = publishLineNumber;
_asyncFrame = asyncFrame;
Body = body;
}
Expand All @@ -378,7 +395,11 @@ public FramedCodeExpression(MSAst.Expression localContext, MSAst.Expression code

public MSAst.Expression Body { get; }

public override MSAst.Expression Reduce() {
public override MSAst.Expression Reduce() => Reduce(publishLineNumber: _publishLineNumber && CanPublishLineNumber);

void IInstructionProvider.AddInstructions(LightCompiler compiler) => compiler.Compile(Reduce(publishLineNumber: false));

private MSAst.Expression Reduce(bool publishLineNumber) {
if (_inGenerator) {
return Ast.Block(
Ast.Call(
Expand Down Expand Up @@ -414,11 +435,7 @@ public override MSAst.Expression Reduce() {
AstUtils.Try(
Ast.Assign(
FunctionStackVariable,
Ast.Call(
AstMethods.PushFrame,
_localContext,
Ast.Assign(code, _codeObject)
)
PushFrame(Ast.Assign(code, _codeObject), publishLineNumber)
),
Body
).Finally(
Expand All @@ -434,29 +451,35 @@ public override MSAst.Expression Reduce() {
return AstUtils.Try(
Ast.Assign(
FunctionStackVariable,
Ast.Call(
AstMethods.PushFrame,
_localContext,
_codeObject
)
PushFrame(_codeObject, publishLineNumber)
),
Body
).Finally(
Ast.Call(
FunctionStackVariable,
typeof(List<FunctionStack>).GetMethod(nameof(List<FunctionStack>.RemoveAt))!,
Ast.Add(
Ast.Property(
FunctionStackVariable,
"Count"
),
Ast.Constant(-1)
)
AstMethods.PopFrame,
FunctionStackVariable
)
);
#endif
}

private MSAst.Expression PushFrame(MSAst.Expression codeObject, bool publishLineNumber) {
if (publishLineNumber) {
return Ast.Call(
AstMethods.PushFrameWithLineNumber,
_localContext,
codeObject,
LineNumberExpression
);
}

return Ast.Call(
AstMethods.PushFrame,
_localContext,
codeObject
);
}

public override Type Type => Body.Type;

public override bool CanReduce => true;
Expand All @@ -468,7 +491,7 @@ protected override MSAst.Expression VisitChildren(ExpressionVisitor visitor) {
var asyncFrame = visitor.Visit(_asyncFrame);

if (localContext != _localContext || _codeObject != codeObject || body != Body || asyncFrame != _asyncFrame) {
return new FramedCodeExpression(localContext, codeObject, body, _inGenerator, asyncFrame);
return new FramedCodeExpression(localContext, codeObject, body, _inGenerator, _publishLineNumber, asyncFrame);
}

return this;
Expand Down
10 changes: 6 additions & 4 deletions src/core/IronPython/Compiler/Ast/PythonAst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,8 @@ internal MSAst.Expression ReduceWorker() {
end.Column
),
AstUtils.Convert(simpleBody, typeof(object))
)
),
publishLineNumber: false // there is no line number variable
Comment thread
BCSharp marked this conversation as resolved.
Resolved
);
}

Expand All @@ -422,7 +423,7 @@ internal MSAst.Expression ReduceWorker() {

MSAst.Expression body = Ast.Block(block.ToReadOnlyCollection());

body = AddModuleFrame(body);
body = AddModuleFrame(body, publishLineNumber: Body.CanThrow);

body = WrapScopeStatements(body, Body.CanThrow); // new ComboActionRewriter().VisitNode(Transform(ag))

Expand All @@ -445,12 +446,13 @@ internal MSAst.Expression ReduceWorker() {
/// <summary>
/// Adds the frame of module, exec or eval code, which the code pushes and pops itself, as functions and class bodies do.
/// </summary>
private MSAst.Expression AddModuleFrame(MSAst.Expression body) {
/// <param name="publishLineNumber">Whether the code has a line number variable to publish (see <see cref="Node.AddFrame"/>).</param>
private MSAst.Expression AddModuleFrame(MSAst.Expression body, bool publishLineNumber) {
if (!PyContext.PythonOptions.Frames) {
return body;
}

return Ast.Block([FunctionStackVariable], AddFrame(LocalContext, FuncCodeExpr, body));
return Ast.Block([FunctionStackVariable], AddFrame(LocalContext, FuncCodeExpr, body, publishLineNumber));
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/core/IronPython/Modules/sys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public static string getfilesystemencodeerrors() {
cur
);

stack[i] = new FunctionStack(elem.Context, elem.Code, cur);
stack[i] = elem.WithFrame(cur);
}
}
return cur;
Expand Down
38 changes: 37 additions & 1 deletion src/core/IronPython/Runtime/Exceptions/TraceBack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ internal TraceBackFrame(CodeContext/*!*/ context, PythonDictionary globals, obje
_locals = locals;
_code = code;
_context = context;
_lineNo = 1;
}

internal TraceBackFrame(CodeContext/*!*/ context, PythonDictionary globals, object locals, FunctionCode code, TraceBackFrame back) {
Expand All @@ -103,6 +104,7 @@ internal TraceBackFrame(CodeContext/*!*/ context, PythonDictionary globals, obje
_code = code;
_context = context;
_back = back;
_lineNo = 1;
}

internal TraceBackFrame(PythonTracebackListener traceAdapter, FunctionCode code, TraceBackFrame back, PythonDebuggingPayload debugProperties, Func<IDictionary<object, object>> scopeCallback) {
Expand Down Expand Up @@ -205,7 +207,7 @@ public object f_lineno {
if (_traceAdapter != null) {
return _lineNo;
} else {
return 1;
return GetLineNumber();
}
}
set {
Expand All @@ -217,6 +219,40 @@ public object f_lineno {
}
}

/// <summary>
/// Gets the current line of a frame that isn't traced.
/// </summary>
/// <remarks>
/// While the code of the frame runs, compiled code publishes its line through the frame stack entry, which is read only on the
/// thread of the entry. Otherwise, the frame reports the line saved by <see cref="SaveLineNumber"/>, or 1 if no line is known.
/// </remarks>
private unsafe int GetLineNumber() {
List<FunctionStack> stack = PythonOps.GetFunctionStackNoCreate();
if (stack != null) {
for (int i = stack.Count - 1; i >= 0; i--) {
FunctionStack entry = stack[i];
if (entry.Frame == this) {
if (entry.LineNumberPtr != null) {
return *entry.LineNumberPtr;
}
break;
}
}
}
return _lineNo;
}

/// <summary>
/// Saves the current line that the code of this frame publishes through <paramref name="lineNumberPtr"/>, the
/// <see cref="FunctionStack.LineNumberPtr"/> of its entry, if any, so that the frame still reports it after the entry is removed
/// from the frame stack.
/// </summary>
internal unsafe void SaveLineNumber(int* lineNumberPtr) {
if (lineNumberPtr != null) {
_lineNo = *lineNumberPtr;
}
}

private void SetLineNumber(int newLineNum) {
if (_traceAdapter == null || PythonTracebackListener.CurrentTraceFrame != this) {
throw PythonOps.ValueError("f_lineno can only be set by a trace function on the frame of the current event, other than a 'return' event");
Expand Down
Loading
Loading