Skip to content
Merged
Prev Previous commit
Next Next commit
Add a couple of asserts for NEXT_LOCATION
  • Loading branch information
markshannon committed Mar 6, 2025
commit 1229037783979e8b5bb2de22e2bfbbcb42ddfe3a
3 changes: 3 additions & 0 deletions Python/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,15 @@ assemble_location_info(struct assembler *a, instr_sequence *instrs,
a->a_lineno = firstlineno;
location loc = NO_LOCATION;
int size = 0;
// The last location should not be NEXT_LOCATION, but don't crash non-debug builds
if (same_location(instrs->s_instrs[instrs->s_used-1].i_loc, NEXT_LOCATION)) {
assert(0 && "last instruction has NEXT_LOCATION");
instrs->s_instrs[instrs->s_used-1].i_loc = NO_LOCATION;
}
for (int i = instrs->s_used-1; i > 0; i--) {
instruction *instr = &instrs->s_instrs[i];
if (same_location(instr[-1].i_loc, NEXT_LOCATION)) {
assert(!IS_TERMINATOR_OPCODE(instr[-1].i_opcode));
instr[-1].i_loc = instr->i_loc;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this transformation should be in propagate_line_numbers in flowgraph.c. Otherwise the last instruction may remain without location, when it could have received a location from an earlier instruction in its block.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be too early in propagate_line_numbers?
If the NEXT_LOCATION instruction is at the end of the block where would it get the location information from?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the next block (fall through or jump). If there is more than one successor, then we have a problem anyway, right, so what do we do?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should only use NEXT_LOCATION if instruction is not the last instruction in the block. I'll add an assert for that.

Expand Down