2013-11-23 18:49:22 -04:00
|
|
|
import dis
|
2020-08-03 13:41:24 -03:00
|
|
|
from test.support.import_helper import import_module
|
2013-11-23 18:49:22 -04:00
|
|
|
import unittest
|
2021-07-29 13:26:53 -03:00
|
|
|
import opcode
|
2013-11-23 18:49:22 -04:00
|
|
|
|
2013-11-23 20:11:17 -04:00
|
|
|
_opcode = import_module("_opcode")
|
2018-09-18 03:54:26 -03:00
|
|
|
from _opcode import stack_effect
|
|
|
|
|
2013-11-23 20:11:17 -04:00
|
|
|
|
2013-11-23 18:49:22 -04:00
|
|
|
class OpcodeTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_stack_effect(self):
|
2018-09-18 03:54:26 -03:00
|
|
|
self.assertEqual(stack_effect(dis.opmap['POP_TOP']), -1)
|
|
|
|
self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 0), -1)
|
|
|
|
self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 1), -1)
|
|
|
|
self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 3), -2)
|
|
|
|
self.assertRaises(ValueError, stack_effect, 30000)
|
|
|
|
self.assertRaises(ValueError, stack_effect, dis.opmap['BUILD_SLICE'])
|
|
|
|
self.assertRaises(ValueError, stack_effect, dis.opmap['POP_TOP'], 0)
|
2018-04-25 16:04:06 -03:00
|
|
|
# All defined opcodes
|
2022-07-01 11:33:35 -03:00
|
|
|
has_arg = dis.hasarg
|
2022-04-19 05:45:08 -03:00
|
|
|
for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()):
|
2018-04-25 16:04:06 -03:00
|
|
|
with self.subTest(opname=name):
|
2022-07-01 11:33:35 -03:00
|
|
|
if code not in has_arg:
|
2018-09-18 03:54:26 -03:00
|
|
|
stack_effect(code)
|
|
|
|
self.assertRaises(ValueError, stack_effect, code, 0)
|
2018-04-25 16:04:06 -03:00
|
|
|
else:
|
2018-09-18 03:54:26 -03:00
|
|
|
stack_effect(code, 0)
|
|
|
|
self.assertRaises(ValueError, stack_effect, code)
|
2018-04-25 16:04:06 -03:00
|
|
|
# All not defined opcodes
|
|
|
|
for code in set(range(256)) - set(dis.opmap.values()):
|
|
|
|
with self.subTest(opcode=code):
|
2018-09-18 03:54:26 -03:00
|
|
|
self.assertRaises(ValueError, stack_effect, code)
|
|
|
|
self.assertRaises(ValueError, stack_effect, code, 0)
|
|
|
|
|
|
|
|
def test_stack_effect_jump(self):
|
|
|
|
JUMP_IF_TRUE_OR_POP = dis.opmap['JUMP_IF_TRUE_OR_POP']
|
|
|
|
self.assertEqual(stack_effect(JUMP_IF_TRUE_OR_POP, 0), 0)
|
|
|
|
self.assertEqual(stack_effect(JUMP_IF_TRUE_OR_POP, 0, jump=True), 0)
|
|
|
|
self.assertEqual(stack_effect(JUMP_IF_TRUE_OR_POP, 0, jump=False), -1)
|
|
|
|
FOR_ITER = dis.opmap['FOR_ITER']
|
|
|
|
self.assertEqual(stack_effect(FOR_ITER, 0), 1)
|
2022-10-27 07:55:03 -03:00
|
|
|
self.assertEqual(stack_effect(FOR_ITER, 0, jump=True), 1)
|
2018-09-18 03:54:26 -03:00
|
|
|
self.assertEqual(stack_effect(FOR_ITER, 0, jump=False), 1)
|
|
|
|
JUMP_FORWARD = dis.opmap['JUMP_FORWARD']
|
|
|
|
self.assertEqual(stack_effect(JUMP_FORWARD, 0), 0)
|
|
|
|
self.assertEqual(stack_effect(JUMP_FORWARD, 0, jump=True), 0)
|
|
|
|
self.assertEqual(stack_effect(JUMP_FORWARD, 0, jump=False), 0)
|
|
|
|
# All defined opcodes
|
2022-07-01 11:33:35 -03:00
|
|
|
has_arg = dis.hasarg
|
|
|
|
has_exc = dis.hasexc
|
2018-09-18 03:54:26 -03:00
|
|
|
has_jump = dis.hasjabs + dis.hasjrel
|
2022-04-19 05:45:08 -03:00
|
|
|
for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()):
|
2018-09-18 03:54:26 -03:00
|
|
|
with self.subTest(opname=name):
|
2022-07-01 11:33:35 -03:00
|
|
|
if code not in has_arg:
|
2018-09-18 03:54:26 -03:00
|
|
|
common = stack_effect(code)
|
|
|
|
jump = stack_effect(code, jump=True)
|
|
|
|
nojump = stack_effect(code, jump=False)
|
|
|
|
else:
|
|
|
|
common = stack_effect(code, 0)
|
|
|
|
jump = stack_effect(code, 0, jump=True)
|
|
|
|
nojump = stack_effect(code, 0, jump=False)
|
2022-07-01 11:33:35 -03:00
|
|
|
if code in has_jump or code in has_exc:
|
2018-09-18 03:54:26 -03:00
|
|
|
self.assertEqual(common, max(jump, nojump))
|
|
|
|
else:
|
|
|
|
self.assertEqual(jump, common)
|
|
|
|
self.assertEqual(nojump, common)
|
2018-04-25 16:04:06 -03:00
|
|
|
|
2013-11-23 18:49:22 -04:00
|
|
|
|
2021-07-29 13:26:53 -03:00
|
|
|
class SpecializationStatsTests(unittest.TestCase):
|
|
|
|
def test_specialization_stats(self):
|
|
|
|
stat_names = opcode._specialization_stats
|
|
|
|
|
|
|
|
specialized_opcodes = [
|
|
|
|
op[:-len("_ADAPTIVE")].lower() for
|
|
|
|
op in opcode._specialized_instructions
|
|
|
|
if op.endswith("_ADAPTIVE")]
|
|
|
|
self.assertIn('load_attr', specialized_opcodes)
|
|
|
|
self.assertIn('binary_subscr', specialized_opcodes)
|
|
|
|
|
|
|
|
stats = _opcode.get_specialization_stats()
|
|
|
|
if stats is not None:
|
|
|
|
self.assertIsInstance(stats, dict)
|
|
|
|
self.assertCountEqual(stats.keys(), specialized_opcodes)
|
|
|
|
self.assertCountEqual(
|
|
|
|
stats['load_attr'].keys(),
|
2021-12-17 10:48:01 -04:00
|
|
|
stat_names + ['failure_kinds'])
|
2021-07-29 13:26:53 -03:00
|
|
|
for sn in stat_names:
|
|
|
|
self.assertIsInstance(stats['load_attr'][sn], int)
|
2021-08-11 13:34:01 -03:00
|
|
|
self.assertIsInstance(
|
2021-12-17 10:48:01 -04:00
|
|
|
stats['load_attr']['failure_kinds'],
|
2021-08-11 13:34:01 -03:00
|
|
|
tuple)
|
2021-12-17 10:48:01 -04:00
|
|
|
for v in stats['load_attr']['failure_kinds']:
|
2021-07-29 13:26:53 -03:00
|
|
|
self.assertIsInstance(v, int)
|
|
|
|
|
|
|
|
|
2013-11-23 18:49:22 -04:00
|
|
|
if __name__ == "__main__":
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|