while1stmt.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (c) 2020, 2022 Rocky Bernstein
  2. def while1stmt(self, lhs, n, rule, ast, tokens, first, last):
  3. # If there is a fall through to the COME_FROM_LOOP, then this is
  4. # not a while 1. So the instruction before should either be a
  5. # JUMP_BACK or the instruction before should not be the target of a
  6. # jump. (Well that last clause i not quite right; that target could be
  7. # from dead code. Ugh. We need a more uniform control flow analysis.)
  8. if last == n or tokens[last - 1] == "COME_FROM_LOOP":
  9. cfl = last - 1
  10. else:
  11. cfl = last
  12. assert tokens[cfl] == "COME_FROM_LOOP"
  13. for loop_end in range(cfl - 1, first, -1):
  14. if tokens[loop_end] != "POP_BLOCK":
  15. break
  16. if tokens[loop_end].kind not in ("JUMP_BACK", "RETURN_VALUE", "RAISE_VARARGS_1"):
  17. if not tokens[loop_end].kind.startswith("COME_FROM"):
  18. return True
  19. # Check that the SETUP_LOOP jumps to the offset after the
  20. # COME_FROM_LOOP
  21. if 0 <= last and tokens[last] in ("COME_FROM_LOOP", "JUMP_BACK"):
  22. # jump_back should be right before COME_FROM_LOOP?
  23. last += 1
  24. if last == n:
  25. last -= 1
  26. offset = tokens[last].off2int()
  27. assert tokens[first] == "SETUP_LOOP"
  28. # Scan for jumps out of the loop. Skip the initial "SETUP_LOOP" instruction.
  29. # If there is a JUMP_BACK at the end, jumping to that is not breaking out
  30. # of the loop. However after that, any "POP_BLOCK"s or "COME_FROM_LOOP"s
  31. # are considered to break out of the loop.
  32. if tokens[loop_end] == "JUMP_BACK":
  33. loop_end += 1
  34. loop_end_offset = tokens[loop_end].off2int(prefer_last=False)
  35. for t in range(first + 1, loop_end):
  36. token = tokens[t]
  37. # token could be a pseudo-op like "LOAD_STR", which is not in
  38. # token.opc. We will replace that with LOAD_CONST as an
  39. # example of an instruction that is not in token.opc.JUMP_OPS
  40. if token.opc.opmap.get(token.kind, "LOAD_CONST") in token.opc.JUMP_OPS:
  41. if token.attr >= loop_end_offset:
  42. return True
  43. # SETUP_LOOP location must jump either to the last token or the token after the last one
  44. return tokens[first].attr not in (offset, offset + 2)