10000 gh-105481: remove HAS_ARG, HAS_CONST, IS_JUMP_OPCODE, IS_PSEUDO_OPCODE and replace by their new versions by iritkatriel · Pull Request #105865 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-105481: remove HAS_ARG, HAS_CONST, IS_JUMP_OPCODE, IS_PSEUDO_OPCODE and replace by their new versions #105865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove HAS_ARG, HAS_CONST, IS_JUMP_OPCODE macros
  • Loading branch information
iritkatriel committed Jun 16, 2023
commit 0088ef9329ccdaece1a8af10eb072cad034b9aa4
13 changes: 0 additions & 13 deletions Include/internal/pycore_opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions Include/internal/pycore_opcode_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ extern "C" {
(((opcode) >= 0 && (opcode) <= MAX_REAL_OPCODE) || \
IS_PSEUDO_INSTR(opcode))

#define IS_JUMP_OPCODE(opcode) \
is_bit_set_in_table(_PyOpcode_Jump, opcode)

#define IS_BLOCK_PUSH_OPCODE(opcode) \
((opcode) == SETUP_FINALLY || \
(opcode) == SETUP_WITH || \
Expand Down Expand Up @@ -55,27 +52,6 @@ extern "C" {
(opcode) == RAISE_VARARGS || \
(opcode) == RERAISE)

#define LOG_BITS_PER_INT 5
#define MASK_LOW_LOG_BITS 31

static inline int
is_bit_set_in_table(const uint32_t *table, int bitindex) {
/* Is the relevant bit set in the relevant word? */
/* 512 bits fit into 9 32-bits words.
* Word is indexed by (bitindex>>ln(size of int in bits)).
* Bit within word is the low bits of bitindex.
*/
if (bitindex >= 0 && bitindex < 512) {
uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
}
else {
return 0;
}
}

#undef LOG_BITS_PER_INT
#undef MASK_LOW_LOG_BITS

/* Flags used in the oparg for MAKE_FUNCTION */
#define MAKE_FUNCTION_DEFAULTS 0x01
Expand Down
17 changes: 0 additions & 17 deletions Include/opcode.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,14 @@ def test_stack_effect(self):
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)
# All defined opcodes
has_arg = dis.hasarg
for name, code in filter(lambda item: item[0] not in dis.deoptmap, dis.opmap.items()):
if code >= opcode.MIN_INSTRUMENTED_OPCODE:
continue
with self.subTest(opname=name):
if code not in has_arg:
stack_effect(code)
self.assertRaises(ValueError, stack_effect, code, 0)
else:
stack_effect(code, 0)
self.assertRaises(ValueError, stack_effect, code)
stack_effect(code)
stack_effect(code, 0)
# All not defined opcodes
for code in set(range(256)) - set(dis.opmap.values()):
with self.subTest(opcode=code):
Expand Down
22 changes: 6 additions & 16 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,16 @@ _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
PyObject *jump)
/*[clinic end generated code: output=64a18f2ead954dbb input=461c9d4a44851898]*/
{
int effect;
int oparg_int = 0;
int jump_int;
if (HAS_ARG(opcode)) {
if (oparg == Py_None) {
PyErr_SetString(PyExc_ValueError,
"stack_effect: opcode requires oparg but oparg was not specified");
return -1;
}

if (oparg != Py_None) {
oparg_int = (int)PyLong_AsLong(oparg);
if ((oparg_int == -1) && PyErr_Occurred()) {
return -1;
}
}
else if (oparg != Py_None) {
PyErr_SetString(PyExc_ValueError,
"stack_effect: opcode does not permit oparg but oparg was specified");
return -1;
}

if (jump == Py_None) {
jump_int = -1;
}
Expand All @@ -60,11 +51,10 @@ _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,
"stack_effect: jump must be False, True or None");
return -1;
}
effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
int effect = PyCompile_OpcodeStackEffectWithJump(opcode, oparg_int, jump_int);
if (effect == PY_INVALID_STACK_EFFECT) {
PyErr_SetString(PyExc_ValueError,
"invalid opcode or oparg");
return -1;
PyErr_SetString(PyExc_ValueError, "invalid opcode or oparg");
return -1;
}
return effect;
}
Expand Down
2 changes: 1 addition & 1 deletion Python/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ write_instr(_Py_CODEUNIT *codestr, instruction *instr, int ilen)
assert(IS_PSEUDO_OPCODE(opcode) == IS_PSEUDO_INSTR(opcode));
assert(!IS_PSEUDO_INSTR(opcode));
int oparg = instr->i_oparg;
assert(HAS_ARG(opcode) || oparg == 0);
assert(OPCODE_HAS_ARG(opcode) || oparg == 0);
int caches = _PyOpcode_Caches[opcode];
switch (ilen - caches) {
case 4:
Expand Down
5 changes: 0 additions & 5 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,6 @@ instr_sequence_use_label(instr_sequence *seq, int lbl) {
static int
instr_sequence_addop(instr_sequence *seq, int opcode, int oparg, location loc)
{
/* compare old and new opcode macros - use ! to compare as bools. */
assert(!HAS_ARG(opcode) == !OPCODE_HAS_ARG(opcode));
assert(!HAS_CONST(opcode) == !OPCODE_HAS_CONST(opcode));
assert(!IS_JUMP_OPCODE(opcode) == !OPCODE_HAS_JUMP(opcode));

assert(0 <= opcode && opcode <= MAX_OPCODE);
assert(IS_PSEUDO_OPCODE(opcode) == IS_PSEUDO_INSTR(opcode));
assert(IS_WITHIN_OPCODE_RANGE(opcode));
Expand Down
1 change: 0 additions & 1 deletion Python/flowgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ is_block_push(cfg_instr *i)
static inline int
is_jump(cfg_instr *i)
{
assert(!OPCODE_HAS_JUMP(i->i_opcode) == !IS_JUMP_OPCODE(i->i_opcode));
return OPCODE_HAS_JUMP(i->i_opcode);
}

Expand Down
28 changes: 0 additions & 28 deletions Tools/build/generate_opcode_h.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@

UINT32_MASK = (1<<32)-1

def write_int_array_from_ops(name, ops, out):
bits = 0
for op in ops:
bits |= 1<<op
out.write(f"const uint32_t {name}[9] = {{\n")
for i in range(9):
out.write(f" {bits & UINT32_MASK}U,\n")
bits >>= 32
assert bits == 0
out.write(f"}};\n")

def main(opcode_py, outfile='Include/opcode.h',
internaloutfile='Include/internal/pycore_opcode.h',
Expand All @@ -100,7 +90,6 @@ def main(opcode_py, outfile='Include/opcode.h',
_pseudo_ops = opcode['_pseudo_ops']

ENABLE_SPECIALIZATION = opcode["ENABLE_SPECIALIZATION"]
HAVE_ARGUMENT = opcode["HAVE_ARGUMENT"]
MIN_PSEUDO_OPCODE = opcode["MIN_PSEUDO_OPCODE"]
MAX_PSEUDO_OPCODE = opcode["MAX_PSEUDO_OPCODE"]
MIN_INSTRUMENTED_OPCODE = opcode["MIN_INSTRUMENTED_OPCODE"]
Expand Down Expand Up @@ -130,8 +119,6 @@ def main(opcode_py, outfile='Include/opcode.h',
for name in opname:
if name in opmap:
op = opmap[name]
if op == HAVE_ARGUMENT:
fobj.write(DEFINE.format("HAVE_ARGUMENT", HAVE_ARGUMENT))
if op == MIN_PSEUDO_OPCODE:
fobj.write(DEFINE.format("MIN_PSEUDO_OPCODE", MIN_PSEUDO_OPCODE))
if op == MIN_INSTRUMENTED_OPCODE:
Expand All @@ -146,11 +133,9 @@ def main(opcode_py, outfile='Include/opcode.h',
for name, op in specialized_opmap.items():
fobj.write(DEFINE.format(name, op))

iobj.write("\nextern const uint32_t _PyOpcode_Jump[9];\n")
iobj.write("\nextern const uint8_t _PyOpcode_Caches[256];\n")
iobj.write("\nextern const uint8_t _PyOpcode_Deopt[256];\n")
iobj.write("\n#ifdef NEED_OPCODE_TABLES\n")
write_int_array_from_ops("_PyOpcode_Jump", opcode['hasjrel'] + opcode['hasjabs'], iobj)

iobj.write("\nconst uint8_t _PyOpcode_Caches[256] = {\n")
for i, entries in enumerate(opcode["_inline_cache_entries"]):
Expand All @@ -171,19 +156,6 @@ def main(opcode_py, outfile='Include/opcode.h',
iobj.write("};\n")
iobj.write("#endif // NEED_OPCODE_TABLES\n")

fobj.write("\n")
fobj.write("#define HAS_ARG(op) ((((op) >= HAVE_ARGUMENT) && (!IS_PSEUDO_OPCODE(op)))\\")
for op in _pseudo_ops:
if opmap[op] in hasarg:
fobj.write(f"\n || ((op) == {op}) \\")
fobj.write("\n )\n")

fobj.write("\n")
fobj.write("#define HAS_CONST(op) (false\\")
for op in hasconst:
fobj.write(f"\n || ((op) == {opname[op]}) \\")
fobj.write("\n )\n")

fobj.write("\n")
for i, (op, _) in enumerate(opcode["_nb_ops"]):
fobj.write(DEFINE.format(op, i))
Expand Down
0