Skip to content

Commit 63f6fc0

Browse files
apply review suggestions
1 parent 3c22cb5 commit 63f6fc0

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

Python/optimizer_analysis.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ incorrect_keys(PyObject *obj, uint32_t version)
187187
#define sym_new_truthiness _Py_uop_sym_new_truthiness
188188

189189
#define JUMP_TO_LABEL(label) goto label;
190-
#define CHECK_STACK_BOUNDS() do { \
190+
#define CHECK_STACK_BOUNDS() \
191+
do { \
191192
if (!CURRENT_FRAME_IS_INIT_SHIM() && (STACK_LEVEL() < 0 || STACK_LEVEL() > STACK_SIZE())) { \
192193
ctx->contradiction = true; \
193194
ctx->done = true; \

Tools/cases_generator/optimizer_generator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def __init__(self, out: CWriter, labels: dict[str, Label], original_uop: Uop, st
148148
self.stack = stack
149149

150150
def emit_save(self, storage: Storage) -> None:
151-
storage.flush(self.out, check_stack_bounds=True)
151+
storage.flush(self.out)
152152

153153
def sync_sp(
154154
self,
@@ -162,7 +162,7 @@ def sync_sp(
162162
next(tkn_iter)
163163
next(tkn_iter)
164164
storage.clear_inputs("when syncing stack")
165-
storage.flush(self.out, check_stack_bounds=True)
165+
storage.flush(self.out)
166166
storage.stack.clear(self.out)
167167
return True
168168

@@ -272,7 +272,7 @@ def replace_opcode_if_evaluates_pure(
272272
emitter.emit("}\n")
273273
emitter.emit("}\n")
274274

275-
storage.flush(self.out, check_stack_bounds=True)
275+
storage.flush(self.out)
276276
emitter.emit("break;\n")
277277
emitter.emit("}\n")
278278
return True
@@ -413,12 +413,12 @@ def write_uop(
413413
var.in_local = False
414414
_, storage = emitter.emit_tokens(override, storage, None, False)
415415
out.start_line()
416-
storage.flush(out, check_stack_bounds=True)
416+
storage.flush(out)
417417
out.start_line()
418418
else:
419419
emit_default(out, uop, stack)
420420
out.start_line()
421-
stack.flush(out, check_stack_bounds=True)
421+
stack.flush(out)
422422
except StackError as ex:
423423
raise analysis_error(ex.args[0], prototype.body.open) # from None
424424

@@ -461,7 +461,7 @@ def generate_abstract_interpreter(
461461
declare_variables(override, out, skip_inputs=False)
462462
else:
463463
declare_variables(uop, out, skip_inputs=True)
464-
stack = Stack()
464+
stack = Stack(check_stack_bounds=True)
465465
write_uop(override, uop, out, stack, debug, skip_inputs=(override is None))
466466
out.start_line()
467467
out.emit("break;\n")

Tools/cases_generator/stack.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ def array_or_scalar(var: StackItem | Local) -> str:
216216
return "array" if var.is_array() else "scalar"
217217

218218
class Stack:
219-
def __init__(self) -> None:
219+
def __init__(self, check_stack_bounds: bool = False) -> None:
220220
self.base_offset = PointerOffset.zero()
221221
self.physical_sp = PointerOffset.zero()
222222
self.logical_sp = PointerOffset.zero()
223223
self.variables: list[Local] = []
224+
self.check_stack_bounds = check_stack_bounds
224225

225226
def drop(self, var: StackItem, check_liveness: bool) -> None:
226227
self.logical_sp = self.logical_sp.pop(var)
@@ -291,12 +292,12 @@ def _do_emit(
291292
) -> None:
292293
out.emit(f"stack_pointer[{stack_offset.to_c()}] = {var.name};\n")
293294

294-
def _save_physical_sp(self, out: CWriter, check_stack_bounds: bool) -> None:
295+
def _save_physical_sp(self, out: CWriter) -> None:
295296
if self.physical_sp != self.logical_sp:
296297
diff = self.logical_sp - self.physical_sp
297298
out.start_line()
298299
out.emit(f"stack_pointer += {diff.to_c()};\n")
299-
if check_stack_bounds:
300+
if self.check_stack_bounds:
300301
out.emit("CHECK_STACK_BOUNDS();\n")
301302
else:
302303
out.emit(f"assert(WITHIN_STACK_BOUNDS());\n")
@@ -319,10 +320,10 @@ def save_variables(self, out: CWriter) -> None:
319320
self._print(out)
320321
var_offset = var_offset.push(var.item)
321322

322-
def flush(self, out: CWriter, check_stack_bounds: bool = False) -> None:
323+
def flush(self, out: CWriter) -> None:
323324
self._print(out)
324325
self.save_variables(out)
325-
self._save_physical_sp(out, check_stack_bounds)
326+
self._save_physical_sp(out)
326327
out.start_line()
327328

328329
def is_flushed(self) -> bool:
@@ -350,6 +351,7 @@ def copy(self) -> "Stack":
350351
other.physical_sp = self.physical_sp
351352
other.logical_sp = self.logical_sp
352353
other.variables = [var.copy() for var in self.variables]
354+
other.check_stack_bounds = self.check_stack_bounds
353355
return other
354356

355357
def __eq__(self, other: object) -> bool:
@@ -501,10 +503,10 @@ def locals_cached(self) -> bool:
501503
return True
502504
return False
503505

504-
def flush(self, out: CWriter, check_stack_bounds: bool = False) -> None:
506+
def flush(self, out: CWriter) -> None:
505507
self.clear_dead_inputs()
506508
self._push_defined_outputs()
507-
self.stack.flush(out, check_stack_bounds=check_stack_bounds)
509+
self.stack.flush(out)
508510

509511
def save(self, out: CWriter) -> None:
510512
assert self.spilled >= 0

0 commit comments

Comments
 (0)