1# (C) Copyright 2019-2020 by Rocky Bernstein
2"""
3PYPY 3.6 opcodes
4
5This is a like Python 3.6's opcode.py with some classification
6of stack usage.
7"""
8
9from xdis.opcodes.base import (
10    format_CALL_FUNCTION_pos_name_encoded,
11    def_op,
12    extended_format_ATTR,
13    extended_format_RAISE_VARARGS_older,
14    extended_format_RETURN_VALUE,
15    finalize_opcodes,
16    format_RAISE_VARARGS_older,
17    format_extended_arg,
18    init_opdata,
19    jrel_op,
20    name_op,
21    nargs_op,
22    rm_op,
23    varargs_op,
24    update_pj3,
25)
26
27version = 3.6
28python_implementation = "PyPy"
29
30from xdis.opcodes.opcode_33 import extended_format_MAKE_FUNCTION
31import xdis.opcodes.opcode_36 as opcode_36
32from xdis.opcodes.opcode_36 import (
33    format_MAKE_FUNCTION_flags,
34)
35
36l = locals()
37init_opdata(l, opcode_36, version, is_pypy=True)
38
39## FIXME: DRY common PYPY opcode additions
40
41# Opcodes removed from 3.6.
42
43rm_op(l, "CALL_FUNCTION_EX", 142)
44rm_op(l, "BUILD_TUPLE_UNPACK_WITH_CALL", 158)
45
46# The following were removed from 3.6 but still in Pypy 3.6
47def_op(l, "MAKE_CLOSURE", 134, 9, 1)  # TOS is number of items to pop
48nargs_op(l, "CALL_FUNCTION_VAR", 140, 9, 1)  # #args + (#kwargs << 8)
49nargs_op(l, "CALL_FUNCTION_KW", 141, 9, 1)  # #args + (#kwargs << 8)
50nargs_op(l, "CALL_FUNCTION_VAR_KW", 142, 9, 1)  # #args + (#kwargs << 8)
51
52# PyPy only
53# ----------
54
55name_op(l, "LOOKUP_METHOD", 201, 1, 2)
56nargs_op(l, "CALL_METHOD", 202, -1, 1)
57l["hasvargs"].append(202)
58
59
60# Used only in single-mode compilation list-comprehension generators
61varargs_op(l, "BUILD_LIST_FROM_ARG", 203)
62
63# Used only in assert statements
64jrel_op(l, "JUMP_IF_NOT_DEBUG", 204, conditional=True)
65
66# PyPy 3.6.1 (and 2.7.13) start to introduce LOAD_REVDB_VAR
67import sys
68
69if sys.version_info[:3] >= (3, 6, 1):
70    def_op(l, "LOAD_REVDB_VAR", 205)
71
72# FIXME remove (fix uncompyle6)
73update_pj3(globals(), l)
74
75opcode_arg_fmt = {
76    "EXTENDED_ARG": format_extended_arg,
77    "MAKE_FUNCTION": format_MAKE_FUNCTION_flags,
78    "RAISE_VARARGS": format_RAISE_VARARGS_older,
79    'CALL_FUNCTION': format_CALL_FUNCTION_pos_name_encoded,
80}
81
82opcode_extended_fmt = {
83    "LOAD_ATTR": extended_format_ATTR,
84    "MAKE_FUNCTION": extended_format_MAKE_FUNCTION,
85    "RAISE_VARARGS": extended_format_RAISE_VARARGS_older,
86    "RETURN_VALUE": extended_format_RETURN_VALUE,
87    "STORE_ATTR": extended_format_ATTR,
88}
89
90finalize_opcodes(l)
91