gh-98831: Modernize CALL_FUNCTION_EX (#101627)

New generator feature: Move CHECK_EVAL_BREAKER() call to just before DISPATCH().
This commit is contained in:
Guido van Rossum 2023-02-07 20:03:22 -08:00 committed by GitHub
parent 790ff6bc6a
commit a9f01448a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 37 deletions

View file

@ -227,7 +227,8 @@ class Instruction:
self.kind = inst.kind
self.name = inst.name
self.block = inst.block
self.block_text, self.predictions = extract_block_text(self.block)
self.block_text, self.check_eval_breaker, self.predictions = \
extract_block_text(self.block)
self.always_exits = always_exits(self.block_text)
self.cache_effects = [
effect for effect in inst.inputs if isinstance(effect, parser.CacheEffect)
@ -1027,6 +1028,8 @@ class Analyzer:
if not instr.always_exits:
for prediction in instr.predictions:
self.out.emit(f"PREDICT({prediction});")
if instr.check_eval_breaker:
self.out.emit("CHECK_EVAL_BREAKER();")
self.out.emit(f"DISPATCH();")
def write_super(self, sup: SuperInstruction) -> None:
@ -1102,7 +1105,7 @@ class Analyzer:
self.out.emit(f"DISPATCH();")
def extract_block_text(block: parser.Block) -> tuple[list[str], list[str]]:
def extract_block_text(block: parser.Block) -> tuple[list[str], bool, list[str]]:
# Get lines of text with proper dedent
blocklines = block.text.splitlines(True)
@ -1122,6 +1125,12 @@ def extract_block_text(block: parser.Block) -> tuple[list[str], list[str]]:
while blocklines and not blocklines[-1].strip():
blocklines.pop()
# Separate CHECK_EVAL_BREAKER() macro from end
check_eval_breaker = \
blocklines != [] and blocklines[-1].strip() == "CHECK_EVAL_BREAKER();"
if check_eval_breaker:
del blocklines[-1]
# Separate PREDICT(...) macros from end
predictions: list[str] = []
while blocklines and (
@ -1130,7 +1139,7 @@ def extract_block_text(block: parser.Block) -> tuple[list[str], list[str]]:
predictions.insert(0, m.group(1))
blocklines.pop()
return blocklines, predictions
return blocklines, check_eval_breaker, predictions
def always_exits(lines: list[str]) -> bool: