1# (C) Copyright 2016-2017, 2020 by Rocky Bernstein
2#
3#  This program is free software; you can redistribute it and/or
4#  modify it under the terms of the GNU General Public License
5#  as published by the Free Software Foundation; either version 2
6#  of the License, or (at your option) any later version.
7#
8#  This program is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#  GNU General Public License for more details.
12#
13#  You should have received a copy of the GNU General Public License
14#  along with this program; if not, write to the Free Software
15#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16"""
17CPython 3.5 bytecode opcodes
18
19This is a like Python 3.5's opcode.py with some classification
20of stack usage.
21"""
22
23from xdis.opcodes.base import (
24    def_op,
25    extended_format_ATTR,
26    extended_format_CALL_FUNCTION,
27    extended_format_RAISE_VARARGS_older,
28    extended_format_RETURN_VALUE,
29    finalize_opcodes,
30    format_CALL_FUNCTION_pos_name_encoded,
31    format_RAISE_VARARGS_older,
32    format_extended_arg,
33    init_opdata,
34    jrel_op,
35    rm_op,
36    update_pj3,
37    varargs_op,
38)
39
40import xdis.opcodes.opcode_34 as opcode_34
41from xdis.opcodes.opcode_33 import (
42    extended_format_MAKE_FUNCTION,
43    format_MAKE_FUNCTION_default_pos_arg,
44)
45
46version = 3.5
47python_implementation = "CPython"
48
49l = locals()
50
51init_opdata(l, opcode_34, version)
52
53# These are removed since Python 3.5.
54# Removals happen before adds since
55# some opcodes are reused
56rm_op(l, "STORE_MAP",                    54)
57rm_op(l, "WITH_CLEANUP",                 81)
58
59# Stack effects are change from 3.4
60varargs_op(l, "BUILD_MAP",              105, -1, -1)  # arg is count of kwarg items
61
62# These are new since Python 3.5
63#          OP NAME                   OPCODE POP PUSH
64#---------------------------------------------------
65def_op(l, "BINARY_MATRIX_MULTIPLY",      16,  2,  1)
66def_op(l, "INPLACE_MATRIX_MULTIPLY",     17,  2,  1)
67def_op(l, "GET_AITER",                   50,  1,  1)
68def_op(l, "GET_ANEXT",                   51,  0,  1)
69def_op(l, "BEFORE_ASYNC_WITH",           52,  0,  1)
70def_op(l, "GET_YIELD_FROM_ITER",         69,  1,  1)
71def_op(l, "GET_AWAITABLE",               73,  0,  0)
72def_op(l, "WITH_CLEANUP_START",          81,  0,  1)
73def_op(l, "WITH_CLEANUP_FINISH",         82,  1,  0)
74
75varargs_op(l, "BUILD_LIST_UNPACK",          149, -1,  1)
76varargs_op(l, "BUILD_MAP_UNPACK",           150, -1,  1)
77varargs_op(l, "BUILD_MAP_UNPACK_WITH_CALL", 151, -1,  1)
78varargs_op(l, "BUILD_TUPLE_UNPACK",         152, -1,  1)
79varargs_op(l, "BUILD_SET_UNPACK",           153, -1,  1)
80
81jrel_op(l, "SETUP_ASYNC_WITH",          154,  0,  6)
82
83update_pj3(globals(), l)
84
85def format_BUILD_MAP_UNPACK_WITH_CALL(oparg):
86    """The lowest byte of oparg is the count of mappings, the relative
87    position of the corresponding callable f is encoded in the second byte
88    of oparg."""
89    rel_func_pos, count = divmod(oparg, 256)
90    return ("%d mappings, function at %d" % (count, count + rel_func_pos))
91
92opcode_arg_fmt = {
93    "BUILD_MAP_UNPACK_WITH_CALL": format_BUILD_MAP_UNPACK_WITH_CALL,
94    "CALL_FUNCTION": format_CALL_FUNCTION_pos_name_encoded,
95    "CALL_FUNCTION_KW": format_CALL_FUNCTION_pos_name_encoded,
96    "CALL_FUNCTION_VAR_KW": format_CALL_FUNCTION_pos_name_encoded,
97    "EXTENDED_ARG": format_extended_arg,
98    "MAKE_FUNCTION": format_MAKE_FUNCTION_default_pos_arg,
99    "RAISE_VARARGS": format_RAISE_VARARGS_older
100}
101
102opcode_extended_fmt = {
103    "CALL_FUNCTION": extended_format_CALL_FUNCTION,
104    "LOAD_ATTR": extended_format_ATTR,
105    "MAKE_FUNCTION": extended_format_MAKE_FUNCTION,
106    "RAISE_VARARGS": extended_format_RAISE_VARARGS_older,
107    "RETURN_VALUE": extended_format_RETURN_VALUE,
108    "STORE_ATTR": extended_format_ATTR,
109}
110
111finalize_opcodes(l)
112