diff --git a/pythonbpf/expr/__init__.py b/pythonbpf/expr/__init__.py index 12da856..040c035 100644 --- a/pythonbpf/expr/__init__.py +++ b/pythonbpf/expr/__init__.py @@ -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", ] diff --git a/pythonbpf/expr/expr_pass.py b/pythonbpf/expr/expr_pass.py index ab85690..d677f2a 100644 --- a/pythonbpf/expr/expr_pass.py +++ b/pythonbpf/expr/expr_pass.py @@ -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 diff --git a/pythonbpf/expr/type_inference.py b/pythonbpf/expr/type_inference.py index 3c10555..9a0c56b 100644 --- a/pythonbpf/expr/type_inference.py +++ b/pythonbpf/expr/type_inference.py @@ -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) diff --git a/pythonbpf/functions/functions_pass.py b/pythonbpf/functions/functions_pass.py index 3f9a3c6..7da4a68 100644 --- a/pythonbpf/functions/functions_pass.py +++ b/pythonbpf/functions/functions_pass.py @@ -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. """