summaryrefslogtreecommitdiffstats
path: root/Python/compile.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2023-01-05 21:01:07 (GMT)
committerGitHub <noreply@github.com>2023-01-05 21:01:07 (GMT)
commit14b7f00fdf9890739b43a3e198e4ce93f54c0552 (patch)
tree45a856294fd582066fd2a38ab33c29457a79f9e4 /Python/compile.c
parent28187141cc34063ef857976ddbca87ba09a882c2 (diff)
downloadcpython-14b7f00fdf9890739b43a3e198e4ce93f54c0552.zip
cpython-14b7f00fdf9890739b43a3e198e4ce93f54c0552.tar.gz
cpython-14b7f00fdf9890739b43a3e198e4ce93f54c0552.tar.bz2
GH-98831: Update generate_cases.py: register inst, opcode_metadata.h (#100735)
(These aren't used yet, but may be coming soon, and it's easier to keep this tool the same between branches.) Added a sanity check for all this to compile.c. Co-authored-by: Irit Katriel <iritkatriel@yahoo.com>
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/Python/compile.c b/Python/compile.c
index ff29fb4..e780446 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -36,6 +36,8 @@
#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
#include "pycore_symtable.h" // PySTEntryObject
+#include "opcode_metadata.h" // _PyOpcode_opcode_metadata
+
#define DEFAULT_BLOCK_SIZE 16
#define DEFAULT_CODE_SIZE 128
@@ -8665,6 +8667,31 @@ no_redundant_jumps(cfg_builder *g) {
}
static bool
+opcode_metadata_is_sane(cfg_builder *g) {
+ for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
+ for (int i = 0; i < b->b_iused; i++) {
+ struct instr *instr = &b->b_instr[i];
+ int opcode = instr->i_opcode;
+ assert(opcode <= MAX_REAL_OPCODE);
+ int pushed = _PyOpcode_opcode_metadata[opcode].n_pushed;
+ int popped = _PyOpcode_opcode_metadata[opcode].n_popped;
+ assert((pushed < 0) == (popped < 0));
+ if (pushed >= 0) {
+ assert(_PyOpcode_opcode_metadata[opcode].valid_entry);
+ int effect = stack_effect(opcode, instr->i_oparg, -1);
+ if (effect != pushed - popped) {
+ fprintf(stderr,
+ "op=%d: stack_effect (%d) != pushed (%d) - popped (%d)\n",
+ opcode, effect, pushed, popped);
+ return false;
+ }
+ }
+ }
+ }
+ return true;
+}
+
+static bool
no_empty_basic_blocks(cfg_builder *g) {
for (basicblock *b = g->g_entryblock; b != NULL; b = b->b_next) {
if (b->b_iused == 0) {
@@ -8847,6 +8874,7 @@ assemble(struct compiler *c, int addNone)
}
assert(no_redundant_jumps(g));
+ assert(opcode_metadata_is_sane(g));
/* Can't modify the bytecode after computing jump offsets. */
assemble_jump_offsets(g->g_entryblock);