11import ast
22import logging
33from llvmlite import ir
4- from pythonbpf .expr import eval_expr
4+ from pythonbpf .expr import eval_expr , get_base_type_and_depth
55
66logger = logging .getLogger (__name__ )
77
@@ -38,6 +38,9 @@ def handle_variable_assignment(
3838
3939 val , val_type = val_result
4040 if val_type != var_type :
41+ logger .info (f"val = { val } " )
42+ logger .info (f"var = { var_ptr } " )
43+ logger .info (f"truthy { var_type } " )
4144 if isinstance (val_type , ir .IntType ) and isinstance (var_type , ir .IntType ):
4245 # Allow implicit int widening
4346 if val_type .width < var_type .width :
@@ -46,10 +49,24 @@ def handle_variable_assignment(
4649 elif val_type .width > var_type .width :
4750 val = builder .trunc (val , var_type )
4851 logger .info (f"Implicitly truncated int for variable { var_name } " )
52+ elif isinstance (val_type , ir .IntType ) and isinstance (var_type , ir .PointerType ):
53+ ptr_target , ptr_depth = get_base_type_and_depth (var_type )
54+ if ptr_target .width > val_type .width :
55+ val = builder .sext (val , ptr_target )
56+ elif ptr_target .width < val_type .width :
57+ val = builder .trunc (val , ptr_target )
58+
59+ if ptr_depth > 1 :
60+ # NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL
61+ var_ptr_tmp = local_sym_tab [f"{ var_name } _tmp" ].var
62+ builder .store (val , var_ptr_tmp )
63+ val = var_ptr_tmp
4964 else :
5065 logger .error (
5166 f"Type mismatch for variable { var_name } : { val_type } vs { var_type } "
5267 )
68+ logger .error (f"var_type: { isinstance (var_type , ir .PointerType )} " )
69+ logger .error (f"val_type: { isinstance (val_type , ir .IntType )} " )
5370 return False
5471
5572 builder .store (val , var_ptr )
0 commit comments