mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
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:
55
pythonbpf/expr/operators.py
Normal file
55
pythonbpf/expr/operators.py
Normal file
@ -0,0 +1,55 @@
|
||||
"""Every Python operator PythonBPF understands, and the IR it maps to.
|
||||
|
||||
This is the single registry. Binary operators map to IRBuilder method names,
|
||||
comparisons map to icmp predicates, and the unary/boolean operators are listed
|
||||
here even though their lowering is structural (they need convert_to_bool or
|
||||
short-circuit control flow, so they live in expr_pass): if an operator is not
|
||||
in this file, the compiler does not support it. Add new operators here first.
|
||||
"""
|
||||
|
||||
import ast
|
||||
|
||||
# ast.BinOp.op class -> llvmlite IRBuilder method name.
|
||||
# Shared by binary-op evaluation and augmented assignment.
|
||||
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",
|
||||
}
|
||||
|
||||
# ast.Compare op class -> icmp predicate string.
|
||||
COMPARISON_OPS = {
|
||||
ast.Eq: "==",
|
||||
ast.NotEq: "!=",
|
||||
ast.Lt: "<",
|
||||
ast.LtE: "<=",
|
||||
ast.Gt: ">",
|
||||
ast.GtE: ">=",
|
||||
ast.Is: "==",
|
||||
ast.IsNot: "!=",
|
||||
}
|
||||
|
||||
# Lowered structurally in expr_pass (need convert_to_bool / control flow).
|
||||
UNARY_OPS = (ast.Not, ast.USub)
|
||||
BOOL_OPS = (ast.And, ast.Or)
|
||||
|
||||
|
||||
def apply_binop(builder, op, left, right):
|
||||
"""Emit the LLVM instruction for a Python binary operator."""
|
||||
method = BINOP_METHODS.get(type(op))
|
||||
if method is None:
|
||||
raise SyntaxError(f"Unsupported binary operation: {type(op).__name__}")
|
||||
return getattr(builder, method)(left, right)
|
||||
|
||||
|
||||
def comparison_predicate(op):
|
||||
"""icmp predicate for a Python comparison operator, or None if unsupported."""
|
||||
return COMPARISON_OPS.get(type(op))
|
||||
Reference in New Issue
Block a user