Skip to content

Commit

Permalink
Modernize LOAD_GLOBAL_BUILTIN
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Feb 1, 2023
1 parent 995822a commit 6868767
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
17 changes: 5 additions & 12 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ dummy_func(
family(load_global, INLINE_CACHE_ENTRIES_LOAD_GLOBAL) = {
LOAD_GLOBAL,
LOAD_GLOBAL_MODULE,
// LOAD_GLOBAL_BUILTIN,
LOAD_GLOBAL_BUILTIN,
};

inst(LOAD_GLOBAL, (unused/1, unused/1, unused/2, unused/1 -- null if (oparg & 1), v)) {
Expand Down Expand Up @@ -1143,28 +1143,21 @@ dummy_func(
null = NULL;
}

// error: LOAD_GLOBAL has irregular stack effect
inst(LOAD_GLOBAL_BUILTIN) {
inst(LOAD_GLOBAL_BUILTIN, (unused/1, index/1, mod_version/2, bltn_version/1 -- null if (oparg & 1), res)) {
assert(cframe.use_tracing == 0);
DEOPT_IF(!PyDict_CheckExact(GLOBALS()), LOAD_GLOBAL);
DEOPT_IF(!PyDict_CheckExact(BUILTINS()), LOAD_GLOBAL);
PyDictObject *mdict = (PyDictObject *)GLOBALS();
PyDictObject *bdict = (PyDictObject *)BUILTINS();
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
uint32_t mod_version = read_u32(cache->module_keys_version);
uint16_t bltn_version = cache->builtin_keys_version;
DEOPT_IF(mdict->ma_keys->dk_version != mod_version, LOAD_GLOBAL);
DEOPT_IF(bdict->ma_keys->dk_version != bltn_version, LOAD_GLOBAL);
assert(DK_IS_UNICODE(bdict->ma_keys));
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(bdict->ma_keys);
PyObject *res = entries[cache->index].me_value;
res = entries[index].me_value;
DEOPT_IF(res == NULL, LOAD_GLOBAL);
int push_null = oparg & 1;
PEEK(0) = NULL;
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
Py_INCREF(res);
STAT_INC(LOAD_GLOBAL, hit);
STACK_GROW(push_null+1);
SET_TOP(Py_NewRef(res));
null = NULL;
}

inst(DELETE_FAST, (--)) {
Expand Down
22 changes: 13 additions & 9 deletions Python/generated_cases.c.h

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

6 changes: 3 additions & 3 deletions Python/opcode_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ _PyOpcode_num_popped(int opcode, int oparg) {
case LOAD_GLOBAL_MODULE:
return 0;
case LOAD_GLOBAL_BUILTIN:
return -1;
return 0;
case DELETE_FAST:
return 0;
case MAKE_CELL:
Expand Down Expand Up @@ -491,7 +491,7 @@ _PyOpcode_num_pushed(int opcode, int oparg) {
case LOAD_GLOBAL_MODULE:
return ((oparg & 1) ? 1 : 0) + 1;
case LOAD_GLOBAL_BUILTIN:
return -1;
return ((oparg & 1) ? 1 : 0) + 1;
case DELETE_FAST:
return 0;
case MAKE_CELL:
Expand Down Expand Up @@ -771,7 +771,7 @@ struct opcode_metadata {
[LOAD_NAME] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_GLOBAL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0000 },
[LOAD_GLOBAL_MODULE] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0000 },
[LOAD_GLOBAL_BUILTIN] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[LOAD_GLOBAL_BUILTIN] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IBC0000 },
[DELETE_FAST] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[MAKE_CELL] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
[DELETE_DEREF] = { DIR_NONE, DIR_NONE, DIR_NONE, true, INSTR_FMT_IB },
Expand Down

0 comments on commit 6868767

Please sign in to comment.