2013-05-06 19:28:21 -03:00
|
|
|
"""bytecode_helper - support tools for testing correct bytecode generation"""
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import dis
|
|
|
|
import io
|
2024-03-28 05:40:37 -03:00
|
|
|
try:
|
|
|
|
import _testinternalcapi
|
|
|
|
except ImportError:
|
|
|
|
_testinternalcapi = None
|
2013-05-06 19:28:21 -03:00
|
|
|
|
|
|
|
_UNSPECIFIED = object()
|
|
|
|
|
2023-12-13 08:00:21 -04:00
|
|
|
def instructions_with_positions(instrs, co_positions):
|
|
|
|
# Return (instr, positions) pairs from the instrs list and co_positions
|
|
|
|
# iterator. The latter contains items for cache lines and the former
|
|
|
|
# doesn't, so those need to be skipped.
|
|
|
|
|
|
|
|
co_positions = co_positions or iter(())
|
|
|
|
for instr in instrs:
|
|
|
|
yield instr, next(co_positions, ())
|
|
|
|
for _, size, _ in (instr.cache_info or ()):
|
|
|
|
for i in range(size):
|
|
|
|
next(co_positions, ())
|
|
|
|
|
2013-05-06 19:28:21 -03:00
|
|
|
class BytecodeTestCase(unittest.TestCase):
|
|
|
|
"""Custom assertion methods for inspecting bytecode."""
|
|
|
|
|
|
|
|
def get_disassembly_as_string(self, co):
|
|
|
|
s = io.StringIO()
|
|
|
|
dis.dis(co, file=s)
|
|
|
|
return s.getvalue()
|
|
|
|
|
|
|
|
def assertInBytecode(self, x, opname, argval=_UNSPECIFIED):
|
2019-09-12 06:02:59 -03:00
|
|
|
"""Returns instr if opname is found, otherwise throws AssertionError"""
|
2022-09-25 16:55:53 -03:00
|
|
|
self.assertIn(opname, dis.opmap)
|
2013-05-06 19:28:21 -03:00
|
|
|
for instr in dis.get_instructions(x):
|
|
|
|
if instr.opname == opname:
|
|
|
|
if argval is _UNSPECIFIED or instr.argval == argval:
|
|
|
|
return instr
|
|
|
|
disassembly = self.get_disassembly_as_string(x)
|
|
|
|
if argval is _UNSPECIFIED:
|
|
|
|
msg = '%s not found in bytecode:\n%s' % (opname, disassembly)
|
|
|
|
else:
|
|
|
|
msg = '(%s,%r) not found in bytecode:\n%s'
|
|
|
|
msg = msg % (opname, argval, disassembly)
|
|
|
|
self.fail(msg)
|
|
|
|
|
|
|
|
def assertNotInBytecode(self, x, opname, argval=_UNSPECIFIED):
|
2019-09-12 06:02:59 -03:00
|
|
|
"""Throws AssertionError if opname is found"""
|
2022-09-25 16:55:53 -03:00
|
|
|
self.assertIn(opname, dis.opmap)
|
2013-05-06 19:28:21 -03:00
|
|
|
for instr in dis.get_instructions(x):
|
|
|
|
if instr.opname == opname:
|
2016-01-19 03:48:48 -04:00
|
|
|
disassembly = self.get_disassembly_as_string(x)
|
|
|
|
if argval is _UNSPECIFIED:
|
2013-05-06 19:28:21 -03:00
|
|
|
msg = '%s occurs in bytecode:\n%s' % (opname, disassembly)
|
2020-12-17 20:30:29 -04:00
|
|
|
self.fail(msg)
|
2013-05-06 19:28:21 -03:00
|
|
|
elif instr.argval == argval:
|
|
|
|
msg = '(%s,%r) occurs in bytecode:\n%s'
|
|
|
|
msg = msg % (opname, argval, disassembly)
|
2020-12-17 20:30:29 -04:00
|
|
|
self.fail(msg)
|
2022-08-24 07:02:53 -03:00
|
|
|
|
2022-11-14 09:56:40 -04:00
|
|
|
class CompilationStepTestCase(unittest.TestCase):
|
2022-08-24 07:02:53 -03:00
|
|
|
|
|
|
|
HAS_ARG = set(dis.hasarg)
|
|
|
|
HAS_TARGET = set(dis.hasjrel + dis.hasjabs + dis.hasexc)
|
|
|
|
HAS_ARG_OR_TARGET = HAS_ARG.union(HAS_TARGET)
|
|
|
|
|
2023-02-28 07:29:32 -04:00
|
|
|
class Label:
|
|
|
|
pass
|
2022-08-24 07:02:53 -03:00
|
|
|
|
2022-11-14 09:56:40 -04:00
|
|
|
def assertInstructionsMatch(self, actual_, expected_):
|
|
|
|
# get two lists where each entry is a label or
|
2023-02-28 07:29:32 -04:00
|
|
|
# an instruction tuple. Normalize the labels to the
|
|
|
|
# instruction count of the target, and compare the lists.
|
2022-11-14 09:56:40 -04:00
|
|
|
|
|
|
|
self.assertIsInstance(actual_, list)
|
|
|
|
self.assertIsInstance(expected_, list)
|
|
|
|
|
|
|
|
actual = self.normalize_insts(actual_)
|
|
|
|
expected = self.normalize_insts(expected_)
|
|
|
|
self.assertEqual(len(actual), len(expected))
|
|
|
|
|
|
|
|
# compare instructions
|
|
|
|
for act, exp in zip(actual, expected):
|
|
|
|
if isinstance(act, int):
|
|
|
|
self.assertEqual(exp, act)
|
|
|
|
continue
|
|
|
|
self.assertIsInstance(exp, tuple)
|
|
|
|
self.assertIsInstance(act, tuple)
|
|
|
|
# crop comparison to the provided expected values
|
|
|
|
if len(act) > len(exp):
|
|
|
|
act = act[:len(exp)]
|
|
|
|
self.assertEqual(exp, act)
|
2022-08-24 07:02:53 -03:00
|
|
|
|
2023-02-28 07:29:32 -04:00
|
|
|
def resolveAndRemoveLabels(self, insts):
|
|
|
|
idx = 0
|
|
|
|
res = []
|
|
|
|
for item in insts:
|
|
|
|
assert isinstance(item, (self.Label, tuple))
|
|
|
|
if isinstance(item, self.Label):
|
|
|
|
item.value = idx
|
|
|
|
else:
|
|
|
|
idx += 1
|
|
|
|
res.append(item)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2022-08-24 07:02:53 -03:00
|
|
|
def normalize_insts(self, insts):
|
|
|
|
""" Map labels to instruction index.
|
2022-11-14 09:56:40 -04:00
|
|
|
Map opcodes to opnames.
|
2022-08-24 07:02:53 -03:00
|
|
|
"""
|
2023-02-28 07:29:32 -04:00
|
|
|
insts = self.resolveAndRemoveLabels(insts)
|
2022-08-24 07:02:53 -03:00
|
|
|
res = []
|
|
|
|
for item in insts:
|
2023-02-28 07:29:32 -04:00
|
|
|
assert isinstance(item, tuple)
|
|
|
|
opcode, oparg, *loc = item
|
|
|
|
opcode = dis.opmap.get(opcode, opcode)
|
|
|
|
if isinstance(oparg, self.Label):
|
|
|
|
arg = oparg.value
|
|
|
|
else:
|
|
|
|
arg = oparg if opcode in self.HAS_ARG else None
|
|
|
|
opcode = dis.opname[opcode]
|
|
|
|
res.append((opcode, arg, *loc))
|
2022-08-24 07:02:53 -03:00
|
|
|
return res
|
|
|
|
|
2023-05-01 18:29:30 -03:00
|
|
|
def complete_insts_info(self, insts):
|
|
|
|
# fill in omitted fields in location, and oparg 0 for ops with no arg.
|
|
|
|
res = []
|
|
|
|
for item in insts:
|
|
|
|
assert isinstance(item, tuple)
|
|
|
|
inst = list(item)
|
|
|
|
opcode = dis.opmap[inst[0]]
|
|
|
|
oparg = inst[1]
|
|
|
|
loc = inst[2:] + [-1] * (6 - len(inst))
|
|
|
|
res.append((opcode, oparg, *loc))
|
|
|
|
return res
|
|
|
|
|
2022-08-24 07:02:53 -03:00
|
|
|
|
2024-03-28 05:40:37 -03:00
|
|
|
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
|
2022-11-14 09:56:40 -04:00
|
|
|
class CodegenTestCase(CompilationStepTestCase):
|
2022-08-24 07:02:53 -03:00
|
|
|
|
2022-11-14 09:56:40 -04:00
|
|
|
def generate_code(self, ast):
|
2024-03-28 05:40:37 -03:00
|
|
|
insts, _ = _testinternalcapi.compiler_codegen(ast, "my_file.py", 0)
|
2022-11-14 09:56:40 -04:00
|
|
|
return insts
|
2022-08-24 07:02:53 -03:00
|
|
|
|
|
|
|
|
2024-03-28 05:40:37 -03:00
|
|
|
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
|
2022-11-14 09:56:40 -04:00
|
|
|
class CfgOptimizationTestCase(CompilationStepTestCase):
|
|
|
|
|
2023-05-18 18:22:03 -03:00
|
|
|
def get_optimized(self, insts, consts, nlocals=0):
|
2023-02-28 07:29:32 -04:00
|
|
|
insts = self.normalize_insts(insts)
|
2022-11-14 09:56:40 -04:00
|
|
|
insts = self.complete_insts_info(insts)
|
2024-03-28 05:40:37 -03:00
|
|
|
insts = _testinternalcapi.optimize_cfg(insts, consts, nlocals)
|
2022-11-14 09:56:40 -04:00
|
|
|
return insts, consts
|
2023-05-01 18:29:30 -03:00
|
|
|
|
2024-03-28 05:40:37 -03:00
|
|
|
@unittest.skipIf(_testinternalcapi is None, "requires _testinternalcapi")
|
2023-05-01 18:29:30 -03:00
|
|
|
class AssemblerTestCase(CompilationStepTestCase):
|
|
|
|
|
|
|
|
def get_code_object(self, filename, insts, metadata):
|
2024-03-28 05:40:37 -03:00
|
|
|
co = _testinternalcapi.assemble_code_object(filename, insts, metadata)
|
2023-05-01 18:29:30 -03:00
|
|
|
return co
|