Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
36a7d56
gh-105481: add haslocal to _opcode.py. Generate most oplists in opcod…
iritkatriel Jul 14, 2023
75019ca
added HAS_FREE flag. Removed HAS_FREE from the HAS_LOCAL list
iritkatriel Jul 14, 2023
494680a
add hasjump, soft deprecate hasjrel and hasjabs. Fix build bug
iritkatriel Jul 15, 2023
24786fb
generate hasexc from the C macros instead of defining it again
iritkatriel Jul 15, 2023
563b431
typo
iritkatriel Jul 15, 2023
227756f
Merge remote-tracking branch 'upstream/main' into opcode_py
iritkatriel Jul 17, 2023
1b13b96
ascii quotes
iritkatriel Jul 17, 2023
0f5ae32
make clinic
iritkatriel Jul 17, 2023
289fad1
remove oplists
iritkatriel Jul 17, 2023
598ba2a
import less from opcode in build script. _opcode.has_* don't exist pr…
iritkatriel Jul 17, 2023
d2d355e
hascompare in old versions as well
iritkatriel Jul 17, 2023
27eb2cc
remove redundant init
iritkatriel Jul 17, 2023
998024a
📜🤖 Added by blurb_it.
blurb-it[bot] Jul 17, 2023
ffa4ee3
Merge branch 'main' into opcode_py
iritkatriel Jul 17, 2023
ce76a60
remove unnecessary comment
iritkatriel Jul 17, 2023
4194a12
make the tests more precise
iritkatriel Jul 17, 2023
6b8c46b
sort the lists
iritkatriel Jul 17, 2023
e15f7d5
address some of the code review comments
iritkatriel Jul 18, 2023
21fe0a4
remove hard-coded lists from the tests
iritkatriel Jul 18, 2023
75795db
remove opcode metadata file from generated files list so diff are vis…
iritkatriel Jul 18, 2023
1a0a2b2
import _opcode directly into dis, rather than via opcode
iritkatriel Jul 18, 2023
f530469
add stack effect to dis.__all__
iritkatriel Jul 18, 2023
71ffe34
Merge branch 'main' into opcode_py
iritkatriel Jul 18, 2023
d525c53
import _specializations, _specialized_instructions directly from _opc…
iritkatriel Jul 18, 2023
5e1c4a4
update Tools/scripts/summarize_stats.py to use _opcode_metadata inste…
iritkatriel Jul 18, 2023
6077263
Revert "update Tools/scripts/summarize_stats.py to use _opcode_metada…
iritkatriel Jul 18, 2023
98ba11f
Revert "import _specializations, _specialized_instructions directly f…
iritkatriel Jul 18, 2023
cc8e5e1
Revert "add stack effect to dis.__all__"
iritkatriel Jul 18, 2023
31dbfec
Revert "import _opcode directly into dis, rather than via opcode"
iritkatriel Jul 18, 2023
1abf6ca
Merge branch 'main' into opcode_py
iritkatriel Jul 18, 2023
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
generate hasexc from the C macros instead of defining it again
  • Loading branch information
iritkatriel committed Jul 15, 2023
commit 24786fbc33c6118a78c29a224f6e35456b02f4dc
1 change: 1 addition & 0 deletions Include/cpython/compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ PyAPI_FUNC(int) PyUnstable_OpcodeHasName(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasJump(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasFree(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasLocal(int opcode);
PyAPI_FUNC(int) PyUnstable_OpcodeHasExc(int opcode);

8 changes: 3 additions & 5 deletions Lib/opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
cmp_op = ('<', '<=', '==', '!=', '>', '>=')

hascompare = []
hasexc = []


ENABLE_SPECIALIZATION = True

def is_pseudo(op):
return op >= MIN_PSEUDO_OPCODE and op <= MAX_PSEUDO_OPCODE

oplists = [hascompare, hasexc]
oplists = [hascompare]

opmap = {}

Expand Down Expand Up @@ -240,11 +239,8 @@ def pseudo_op(name, op, real_ops):
MIN_PSEUDO_OPCODE = 256

pseudo_op('SETUP_FINALLY', 256, ['NOP'])
hasexc.append(256)
pseudo_op('SETUP_CLEANUP', 257, ['NOP'])
hasexc.append(257)
pseudo_op('SETUP_WITH', 258, ['NOP'])
hasexc.append(258)
pseudo_op('POP_BLOCK', 259, ['NOP'])

pseudo_op('JUMP', 260, ['JUMP_FORWARD', 'JUMP_BACKWARD'])
Expand Down Expand Up @@ -277,6 +273,7 @@ def pseudo_op(name, op, real_ops):
hasjabs = []
hasfree = [op for op in opmap.values() if _opcode.has_free(op)]
haslocal = [op for op in opmap.values() if _opcode.has_local(op)]
hasexc = [op for op in opmap.values() if _opcode.has_exc(op)]
except (ImportError, AttributeError):
hasarg = []
hasconst = []
Expand All @@ -286,6 +283,7 @@ def pseudo_op(name, op, real_ops):
hasjabs = []
hasfree = []
haslocal = []
hasexc = []

_nb_ops = [
("NB_ADD", "+"),
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test__opcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def test_invalid_opcodes(self):
self.check_bool_function_result(_opcode.has_jump, invalid, False)
self.check_bool_function_result(_opcode.has_free, invalid, False)
self.check_bool_function_result(_opcode.has_local, invalid, False)
self.check_bool_function_result(_opcode.has_exc, invalid, False)

def test_is_valid(self):
names = [
Expand Down Expand Up @@ -77,6 +78,12 @@ def test_has_local(self):
self.check_bool_function_result(_opcode.has_local, has_local, True)
self.check_bool_function_result(_opcode.has_local, no_local, False)

def test_has_exc(self):
has_exc = ['SETUP_FINALLY', 'SETUP_WITH', 'SETUP_CLEANUP']
no_exc = ['DELETE_DEREF', 'POP_TOP', 'NOP', 'CACHE']
self.check_bool_function_result(_opcode.has_exc, has_exc, True)
self.check_bool_function_result(_opcode.has_exc, no_exc, False)

def test_stack_effect(self):
self.assertEqual(stack_effect(dis.opmap['POP_TOP']), -1)
self.assertEqual(stack_effect(dis.opmap['BUILD_SLICE'], 0), -1)
Expand Down
20 changes: 18 additions & 2 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ or builtin scopes.

static int
_opcode_has_free_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=d81ae4d79af0ee26 input=6b2ce67a4a4017b1]*/
/*[clinic end generated code: output=d81ae4d79af0ee26 input=f94ce9c2475f3ff0]*/
{
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasFree(opcode);
Expand All @@ -183,9 +183,24 @@ _opcode_has_local_impl(PyObject *module, int opcode)
{
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasLocal(opcode);

}

/*[clinic input]

_opcode.has_exc -> bool

opcode: int

Return True if the opcode sets and exception handler, False otherwise.
Comment thread
iritkatriel marked this conversation as resolved.
Outdated
[clinic start generated code]*/

static int
_opcode_has_exc_impl(PyObject *module, int opcode)
/*[clinic end generated code: output=41b68dff0ec82a52 input=a3f202bb94f233b5]*/
{
return PyUnstable_OpcodeIsValid(opcode) &&
PyUnstable_OpcodeHasExc(opcode);
}

/*[clinic input]

Expand Down Expand Up @@ -215,6 +230,7 @@ opcode_functions[] = {
_OPCODE_HAS_JUMP_METHODDEF
_OPCODE_HAS_FREE_METHODDEF
_OPCODE_HAS_LOCAL_METHODDEF
_OPCODE_HAS_EXC_METHODDEF
_OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
{NULL, NULL, 0, NULL}
};
Expand Down
65 changes: 64 additions & 1 deletion Modules/clinic/_opcode.c.h

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

6 changes: 6 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,12 @@ PyUnstable_OpcodeHasLocal(int opcode)
return OPCODE_HAS_LOCAL(opcode);
}

int
PyUnstable_OpcodeHasExc(int opcode)
{
return IS_BLOCK_PUSH_OPCODE(opcode);
}

static int
codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
{
Expand Down