Skip to content

Commit 46093f8

Browse files
committed
pythongh-130887: remove trailing jump in AArch64 JIT stencils
In order to keep the alignment of the code body, the removed jump and the 0 padding at the end of AArch64 JIT stencils have been replaced with nop instructions.
1 parent 98fa4a4 commit 46093f8

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Tools/jit/_stencils.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,15 @@ def pad(self, alignment: int) -> None:
209209
self.disassembly.append(f"{offset:x}: {' '.join(['00'] * padding)}")
210210
self.body.extend([0] * padding)
211211

212-
def remove_jump(self, *, alignment: int = 1) -> None:
212+
def add_nop(self, alignment: int) -> None:
213+
"""Add a NOP if the offset is not aligned."""
214+
offset = len(self.body)
215+
nop = b"\x1f\x20\x03\xD5"
216+
if offset % alignment:
217+
self.disassembly.append(f"{offset:x}: d503201f\t\t nop")
218+
self.body.extend(nop)
219+
220+
def remove_jump(self) -> None:
213221
"""Remove a zero-length continuation jump, if it exists."""
214222
hole = max(self.holes, key=lambda hole: hole.offset)
215223
match hole:
@@ -244,8 +252,9 @@ def remove_jump(self, *, alignment: int = 1) -> None:
244252
jump = b"\x00\x00\x00\x14"
245253
case _:
246254
return
247-
if self.body[offset:] == jump and offset % alignment == 0:
255+
if self.body[offset:] == jump:
248256
self.body = self.body[:offset]
257+
self.disassembly = self.disassembly[:-2]
249258
self.holes.remove(hole)
250259

251260

@@ -289,8 +298,8 @@ def process_relocations(
289298
self._trampolines.add(ordinal)
290299
hole.addend = ordinal
291300
hole.symbol = None
292-
self.code.remove_jump(alignment=alignment)
293-
self.code.pad(alignment)
301+
self.code.remove_jump()
302+
self.code.add_nop(alignment=alignment)
294303
self.data.pad(8)
295304
for stencil in [self.code, self.data]:
296305
for hole in stencil.holes:

0 commit comments

Comments
 (0)