Core: Unify the Python-operator tables in expr/operators.py

Operator knowledge was spread across three files: the binary-op table (an
op_map dict rebuilt inline on every _handle_binary_op_impl call, then
extracted to ir_ops.apply_binop for augmented assignment), COMPARISON_OPS in
type_normalization.py, and the unary/boolean operators as isinstance chains
in expr_pass.py. Anyone asking "which operators does PythonBPF support" had
to read all three.

They now live in one module. BINOP_METHODS and COMPARISON_OPS are the tables
themselves; UNARY_OPS and BOOL_OPS list the operators whose lowering is
structural (convert_to_bool, short-circuit control flow) so the registry is
complete even where the emission stays in expr_pass. apply_binop moves here
from ir_ops, which goes back to being purely about IR operations. No
behaviour change: 133 passed, 19 xfailed before and after.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BSDVsZH5NtoASyxB8FCtGU
This commit is contained in:
Pragyansh Chaturvedi
2026-09-03 07:39:04 +05:30
parent e8ab1188d8
commit f5c4dece3e
6 changed files with 68 additions and 43 deletions

View File

@ -1,37 +1,9 @@
import ast
import logging
from llvmlite import ir
logger = logging.getLogger(__name__)
BINOP_METHODS = {
ast.Add: "add",
ast.Sub: "sub",
ast.Mult: "mul",
ast.Div: "sdiv",
ast.Mod: "srem",
ast.LShift: "shl",
ast.RShift: "lshr",
ast.BitOr: "or_",
ast.BitXor: "xor",
ast.BitAnd: "and_",
ast.FloorDiv: "udiv",
}
def apply_binop(builder, op, left, right):
"""Emit the LLVM instruction for a Python binary operator.
Shared by binary-op evaluation and augmented assignment so the operator
table exists exactly once.
"""
method = BINOP_METHODS.get(type(op))
if method is None:
raise SyntaxError("Unsupported binary operation")
return getattr(builder, method)(left, right)
def deref_to_depth(func, builder, val, target_depth):
"""Dereference a pointer to a certain depth."""