Skip to content

Commit

Permalink
Rework stack-frame handling in the VM
Browse files Browse the repository at this point in the history
Instead of having the stack frames as `ref` objects that are chained
together via their `next` field, store them in a `seq` in `TCtx`. This
makes it easier to reason about them, and also connects the frames
to their owning context.

While the frame list should ideally be first-in last-out (stack), this
isn't currently possible due to how exception handling is implemented.

In addition, the stack trace now always includes the entry function
and also the number of skipped frames. A nil access error when
no entry function exists (happens when a statement is evaluated)
is also fixed.
  • Loading branch information
zerbina committed Mar 21, 2022
1 parent 122f725 commit fb03691
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 78 deletions.
1 change: 1 addition & 0 deletions compiler/ast/reports.nim
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ type
currentExceptionA*, currentExceptionB*: PNode
traceReason*: ReportKind
stacktrace*: seq[tuple[sym: PSym, location: TLineInfo]]
skipped*: int

of rsemReportCountMismatch,
rsemWrongNumberOfVariables:
Expand Down
12 changes: 9 additions & 3 deletions compiler/front/cli_reporter.nim
Original file line number Diff line number Diff line change
Expand Up @@ -527,13 +527,19 @@ proc reportBody*(conf: ConfigRef, r: SemReport): string =

of rsemVmStackTrace:
result = "stack trace: (most recent call last)\n"
for idx, (sym, loc) in r.stacktrace:
for idx in countdown(r.stacktrace.high, 0):
let (sym, loc) = r.stacktrace[idx]
result.add(
conf.toStr(loc),
" ",
sym.name.s,
if idx == r.stacktrace.high: "" else: "\n"
if sym != nil: sym.name.s else: "???"
)
if r.skipped > 0 and idx == r.stacktrace.high:
# The entry point is always the last element in the list
result.add("\nSkipped ", r.skipped, " entries, calls that led up to printing")

if idx > 0:
result.add("\n")

of rsemVmUnhandledException:
result.addf(
Expand Down
Loading

0 comments on commit fb03691

Please sign in to comment.