summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-05-22 00:46:39 (GMT)
committerGitHub <noreply@github.com>2024-05-22 00:46:39 (GMT)
commit98e855fcc1f1d490c803565e84cb611b3f057e45 (patch)
tree53f19fb0244fa8c43e53534c31842adf5b65ebc2 /Python
parent506b1a3ff66a41c72d205c8e4cba574e439d8e76 (diff)
downloadcpython-98e855fcc1f1d490c803565e84cb611b3f057e45.zip
cpython-98e855fcc1f1d490c803565e84cb611b3f057e45.tar.gz
cpython-98e855fcc1f1d490c803565e84cb611b3f057e45.tar.bz2
gh-119180: Add LOAD_COMMON_CONSTANT opcode (#119321)
The PEP 649 implementation will require a way to load NotImplementedError from the bytecode. @markshannon suggested implementing this by converting LOAD_ASSERTION_ERROR into a more general mechanism for loading constants. This PR adds this new opcode. I will work on the rest of the implementation of the PEP separately. Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Diffstat (limited to 'Python')
-rw-r--r--Python/bytecodes.c14
-rw-r--r--Python/compile.c2
-rw-r--r--Python/executor_cases.c.h15
-rw-r--r--Python/generated_cases.c.h32
-rw-r--r--Python/opcode_targets.h2
-rw-r--r--Python/optimizer_cases.c.h2
6 files changed, 49 insertions, 18 deletions
diff --git a/Python/bytecodes.c b/Python/bytecodes.c
index 434eb80..b48f913 100644
--- a/Python/bytecodes.c
+++ b/Python/bytecodes.c
@@ -1184,8 +1184,18 @@ dummy_func(
}
}
- inst(LOAD_ASSERTION_ERROR, ( -- value)) {
- value = Py_NewRef(PyExc_AssertionError);
+ inst(LOAD_COMMON_CONSTANT, ( -- value)) {
+ // Keep in sync with _common_constants in opcode.py
+ switch(oparg) {
+ case CONSTANT_ASSERTIONERROR:
+ value = PyExc_AssertionError;
+ break;
+ case CONSTANT_NOTIMPLEMENTEDERROR:
+ value = PyExc_NotImplementedError;
+ break;
+ default:
+ Py_FatalError("bad LOAD_COMMON_CONSTANT oparg");
+ }
}
inst(LOAD_BUILD_CLASS, ( -- bc)) {
diff --git a/Python/compile.c b/Python/compile.c
index 79f3baa..6dacfc7 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -3974,7 +3974,7 @@ compiler_assert(struct compiler *c, stmt_ty s)
}
NEW_JUMP_TARGET_LABEL(c, end);
RETURN_IF_ERROR(compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1));
- ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR);
+ ADDOP_I(c, LOC(s), LOAD_COMMON_CONSTANT, CONSTANT_ASSERTIONERROR);
if (s->v.Assert.msg) {
VISIT(c, expr, s->v.Assert.msg);
ADDOP_I(c, LOC(s), CALL, 0);
diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h
index 347a1e6..a3d7af2 100644
--- a/Python/executor_cases.c.h
+++ b/Python/executor_cases.c.h
@@ -1162,9 +1162,20 @@
break;
}
- case _LOAD_ASSERTION_ERROR: {
+ case _LOAD_COMMON_CONSTANT: {
PyObject *value;
- value = Py_NewRef(PyExc_AssertionError);
+ oparg = CURRENT_OPARG();
+ // Keep in sync with _common_constants in opcode.py
+ switch(oparg) {
+ case CONSTANT_ASSERTIONERROR:
+ value = PyExc_AssertionError;
+ break;
+ case CONSTANT_NOTIMPLEMENTEDERROR:
+ value = PyExc_NotImplementedError;
+ break;
+ default:
+ Py_FatalError("bad LOAD_COMMON_CONSTANT oparg");
+ }
stack_pointer[0] = value;
stack_pointer += 1;
break;
diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h
index 96161c5..07d9965 100644
--- a/Python/generated_cases.c.h
+++ b/Python/generated_cases.c.h
@@ -3695,17 +3695,6 @@
DISPATCH();
}
- TARGET(LOAD_ASSERTION_ERROR) {
- frame->instr_ptr = next_instr;
- next_instr += 1;
- INSTRUCTION_STATS(LOAD_ASSERTION_ERROR);
- PyObject *value;
- value = Py_NewRef(PyExc_AssertionError);
- stack_pointer[0] = value;
- stack_pointer += 1;
- DISPATCH();
- }
-
TARGET(LOAD_ATTR) {
frame->instr_ptr = next_instr;
next_instr += 10;
@@ -4251,6 +4240,27 @@
DISPATCH();
}
+ TARGET(LOAD_COMMON_CONSTANT) {
+ frame->instr_ptr = next_instr;
+ next_instr += 1;
+ INSTRUCTION_STATS(LOAD_COMMON_CONSTANT);
+ PyObject *value;
+ // Keep in sync with _common_constants in opcode.py
+ switch(oparg) {
+ case CONSTANT_ASSERTIONERROR:
+ value = PyExc_AssertionError;
+ break;
+ case CONSTANT_NOTIMPLEMENTEDERROR:
+ value = PyExc_NotImplementedError;
+ break;
+ default:
+ Py_FatalError("bad LOAD_COMMON_CONSTANT oparg");
+ }
+ stack_pointer[0] = value;
+ stack_pointer += 1;
+ DISPATCH();
+ }
+
TARGET(LOAD_CONST) {
frame->instr_ptr = next_instr;
next_instr += 1;
diff --git a/Python/opcode_targets.h b/Python/opcode_targets.h
index fa4f1f8..322483f 100644
--- a/Python/opcode_targets.h
+++ b/Python/opcode_targets.h
@@ -22,7 +22,6 @@ static void *opcode_targets[256] = {
&&TARGET_GET_LEN,
&&TARGET_GET_YIELD_FROM_ITER,
&&TARGET_INTERPRETER_EXIT,
- &&TARGET_LOAD_ASSERTION_ERROR,
&&TARGET_LOAD_BUILD_CLASS,
&&TARGET_LOAD_LOCALS,
&&TARGET_MAKE_FUNCTION,
@@ -82,6 +81,7 @@ static void *opcode_targets[256] = {
&&TARGET_LIST_APPEND,
&&TARGET_LIST_EXTEND,
&&TARGET_LOAD_ATTR,
+ &&TARGET_LOAD_COMMON_CONSTANT,
&&TARGET_LOAD_CONST,
&&TARGET_LOAD_DEREF,
&&TARGET_LOAD_FAST,
diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h
index cd4431d..30ed011 100644
--- a/Python/optimizer_cases.c.h
+++ b/Python/optimizer_cases.c.h
@@ -628,7 +628,7 @@
break;
}
- case _LOAD_ASSERTION_ERROR: {
+ case _LOAD_COMMON_CONSTANT: {
_Py_UopsSymbol *value;
value = sym_new_not_null(ctx);
stack_pointer[0] = value;