mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Lower augmented assignment directly instead of desugaring
The first version synthesised an Assign(BinOp(...)) node and fed it back through handle_assign. That is a standard compiler move, but it is not how this compiler works: every pass walks the tree the user wrote, and a node invented mid-codegen is invisible to the allocation pass that already ran, carries no source location for diagnostics, and shows up in logs as a statement nobody typed. handle_aug_assign now resolves the target slot (declared global, local, or struct field), loads it, evaluates the right-hand side, applies the operator, and stores back. Semantic agreement with "x = x op v" no longer rests on shared AST but on shared value-level helpers: the RHS goes through get_operand_value like any other read, and the operator table is extracted from binary-op evaluation into apply_binop so it exists exactly once. Two behavioural refinements fall out: augmented assignment to a global name without a global statement is now the same loud SyntaxError plain assignment gives (previously it slipped past the allocation pass, which only walks Assign), and struct-field targets compute their GEP once rather than twice. Target read precedes RHS evaluation, matching Python. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
This commit is contained in:
@ -1,9 +1,37 @@
|
||||
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."""
|
||||
|
||||
|
||||
Reference in New Issue
Block a user