1# (C) Copyright 2019-2021 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.8 bytecode opcodes
18
19This is a like Python 3.8's opcode.py
20"""
21
22from xdis.opcodes.base import (
23    extended_format_RETURN_VALUE,
24    finalize_opcodes,
25    init_opdata,
26    nargs_op,
27    def_op,
28    jrel_op,
29    rm_op,
30    update_pj3,
31)
32
33from xdis.opcodes.opcode_33 import extended_format_ATTR, extended_format_MAKE_FUNCTION
34import xdis.opcodes.opcode_37 as opcode_37
35from xdis.opcodes.opcode_36 import (
36    extended_format_CALL_FUNCTION,
37    extended_format_CALL_METHOD,
38    format_BUILD_MAP_UNPACK_WITH_CALL,
39    format_CALL_FUNCTION_EX,
40    format_CALL_FUNCTION_KW,
41    format_MAKE_FUNCTION_flags,
42    format_extended_arg36,
43)
44
45version = 3.8
46python_implementation = "CPython"
47
48l = locals()
49
50init_opdata(l, opcode_37, version)
51
52# fmt: off
53# These are removed since 3.7...
54rm_op(l, "BREAK_LOOP",     80)
55rm_op(l, "CONTINUE_LOOP", 119)
56rm_op(l, "SETUP_LOOP",    120)
57rm_op(l, "SETUP_EXCEPT",  121)
58
59# These are new/changed since Python 3.7
60
61#          OP NAME            OPCODE POP PUSH
62# --------------------------------------------
63def_op(l, "ROT_FOUR",          6,      4, 4)  # Opcode number changed from 5 to 6. Why?
64def_op(l, "BEGIN_FINALLY",     53,     0, 6)
65def_op(l, "END_ASYNC_FOR",     54,     7, 0)  # POP is 0, when not 7
66def_op(l, "END_FINALLY",       88,     6, 0)  # POP is 6, when not 1
67jrel_op(l, "CALL_FINALLY",    162,     0, 1)
68nargs_op(l, "POP_FINALLY",    163,     6, 0)  # PUSH/POP vary
69
70
71format_value_flags = opcode_37.format_value_flags
72
73opcode_arg_fmt = {
74    "BUILD_MAP_UNPACK_WITH_CALL": format_BUILD_MAP_UNPACK_WITH_CALL,
75    "CALL_FUNCTION_EX":           format_CALL_FUNCTION_EX,
76    "CALL_FUNCTION_KW":           format_CALL_FUNCTION_KW,
77    "EXTENDED_ARG":               format_extended_arg36,
78    "FORMAT_VALUE":               format_value_flags,
79    "MAKE_FUNCTION":              format_MAKE_FUNCTION_flags,
80    "RAISE_VARARGS":              opcode_37.format_RAISE_VARARGS,
81}
82
83opcode_extended_fmt = {
84    "CALL_FUNCTION":              extended_format_CALL_FUNCTION,
85    "CALL_METHOD":                extended_format_CALL_METHOD,
86    "LOAD_ATTR":                  extended_format_ATTR,
87    "MAKE_FUNCTION":              extended_format_MAKE_FUNCTION,
88    "RAISE_VARARGS":              opcode_37.extended_format_RAISE_VARARGS,
89    "RETURN_VALUE":               extended_format_RETURN_VALUE,
90    "STORE_ATTR":                 extended_format_ATTR,
91}
92# fmt: on
93update_pj3(globals(), l)
94
95finalize_opcodes(l)
96