2013-05-06 19:28:21 -03:00
|
|
|
"""bytecode_helper - support tools for testing correct bytecode generation"""
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
import dis
|
|
|
|
import io
|
2022-11-14 09:56:40 -04:00
|
|
|
from _testinternalcapi import compiler_codegen, optimize_cfg
|
2013-05-06 19:28:21 -03:00
|
|
|
|
|
|
|
_UNSPECIFIED = object()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
insts = compiler_codegen(ast, "my_file.py", 0)
|
|
|
|
return insts
|
2022-08-24 07:02:53 -03:00
|
|
|
|
|
|
|
|
2022-11-14 09:56:40 -04:00
|
|
|
class CfgOptimizationTestCase(CompilationStepTestCase):
|
|
|
|
|
|
|
|
def complete_insts_info(self, insts):
|
|
|
|
# fill in omitted fields in location, and oparg 0 for ops with no arg.
|
2023-02-28 07:29:32 -04:00
|
|
|
res = []
|
2022-11-14 09:56:40 -04:00
|
|
|
for item in insts:
|
2023-02-28 07:29:32 -04:00
|
|
|
assert isinstance(item, tuple)
|
|
|
|
inst = list(reversed(item))
|
|
|
|
opcode = dis.opmap[inst.pop()]
|
2023-03-13 12:59:20 -03:00
|
|
|
oparg = inst.pop()
|
2023-02-28 07:29:32 -04:00
|
|
|
loc = inst + [-1] * (4 - len(inst))
|
|
|
|
res.append((opcode, oparg, *loc))
|
|
|
|
return res
|
2022-11-14 09:56:40 -04:00
|
|
|
|
|
|
|
def get_optimized(self, insts, consts):
|
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)
|
|
|
|
insts = optimize_cfg(insts, consts)
|
|
|
|
return insts, consts
|