mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 01:48:35 +00:00
Core: Review nits on the signedness branch
- A global's descriptor is built from the loaded value, not from None. - get_operand_value, the [0] wrapper over get_typed_operand, had no callers left; removed with its export. - infer_int_type's map branch names the map's declared value type for what it is (value_ctype), says that the key argument plays no part, and applies only to lookup: update and delete return a status, not a value of the map's type. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
from .expr_pass import eval_expr, handle_expr, get_operand_value, get_typed_operand
|
||||
from .expr_pass import eval_expr, handle_expr, get_typed_operand
|
||||
from .type_normalization import (
|
||||
convert_to_bool,
|
||||
get_base_type_and_depth,
|
||||
@ -25,7 +25,6 @@ __all__ = [
|
||||
"deref_to_depth",
|
||||
"apply_binop",
|
||||
"access_struct_field",
|
||||
"get_operand_value",
|
||||
"CallHandlerRegistry",
|
||||
"VmlinuxHandlerRegistry",
|
||||
]
|
||||
|
||||
@ -237,7 +237,8 @@ def get_typed_operand(func, compilation_context, operand, builder, local_sym_tab
|
||||
)
|
||||
elif operand.id in compilation_context.bpf_globals:
|
||||
sym = compilation_context.bpf_globals[operand.id]
|
||||
return builder.load(sym.var), _descriptor(None, sym.ir_type)
|
||||
val = builder.load(sym.var)
|
||||
return val, _descriptor(val, sym.ir_type)
|
||||
else:
|
||||
vmlinux_result = VmlinuxHandlerRegistry.handle_name(operand.id)
|
||||
if vmlinux_result is not None:
|
||||
@ -265,13 +266,6 @@ def get_typed_operand(func, compilation_context, operand, builder, local_sym_tab
|
||||
raise TypeError(f"Unsupported operand type: {type(operand)}")
|
||||
|
||||
|
||||
def get_operand_value(func, compilation_context, operand, builder, local_sym_tab):
|
||||
"""Extract the value from an operand, handling variables and constants."""
|
||||
return get_typed_operand(
|
||||
func, compilation_context, operand, builder, local_sym_tab
|
||||
)[0]
|
||||
|
||||
|
||||
def _handle_binary_op_impl(func, compilation_context, rval, builder, local_sym_tab):
|
||||
"""A binary operation, typed per node the way C types it: the operation is
|
||||
performed in the type given by the usual arithmetic conversions of its two
|
||||
|
||||
@ -66,16 +66,21 @@ def infer_int_type(expr, local_sym_tab, compilation_context):
|
||||
return _as_intty(HelperHandlerRegistry.get_return_type(name))
|
||||
return None
|
||||
|
||||
if isinstance(expr, ast.Call) and isinstance(expr.func, ast.Attribute):
|
||||
# map.lookup(k) used as a value: the map's declared value type
|
||||
if (
|
||||
isinstance(expr, ast.Call)
|
||||
and isinstance(expr.func, ast.Attribute)
|
||||
and expr.func.attr == "lookup"
|
||||
):
|
||||
# m.lookup(...) used as a value has the type the map declares for its
|
||||
# values, whatever the key argument looks like. The declaration is a
|
||||
# ctypes name for a scalar map and a struct name otherwise, which is
|
||||
# not an integer, so None.
|
||||
map_name = getattr(expr.func.value, "id", None)
|
||||
sym = compilation_context.map_sym_tab.get(map_name)
|
||||
value = (sym.params or {}).get("value") if sym else None
|
||||
return (
|
||||
_as_intty(ctypes_to_ir(value))
|
||||
if isinstance(value, str) and is_ctypes(value)
|
||||
else None
|
||||
)
|
||||
value_ctype = (sym.params or {}).get("value") if sym else None
|
||||
if isinstance(value_ctype, str) and is_ctypes(value_ctype):
|
||||
return _as_intty(ctypes_to_ir(value_ctype))
|
||||
return None
|
||||
|
||||
if isinstance(expr, ast.Attribute) and isinstance(expr.value, ast.Name):
|
||||
base = local_sym_tab.get(expr.value.id)
|
||||
|
||||
@ -203,7 +203,7 @@ def handle_aug_assign(func, compilation_context, builder, stmt, local_sym_tab):
|
||||
compiler walks the tree the user wrote, and nodes invented mid-codegen are
|
||||
invisible to the passes that already ran and carry no source locations.
|
||||
Semantic agreement with `x = x op v` comes from sharing the value-level
|
||||
helpers instead — the RHS goes through get_operand_value like any other
|
||||
helpers instead — the RHS goes through get_typed_operand like any other
|
||||
read, and the operator table is apply_binop, the same one binary-op
|
||||
evaluation uses.
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user