33from logging import Logger
44import logging
55
6- logger : Logger = logging .getLogger (__name__ )
7-
8-
9- def deref_to_val (var , builder ):
10- """Dereference a variable to get its value and pointer chain."""
11- logger .info (f"Dereferencing { var } , type is { var .type } " )
12-
13- chain = [var ]
14- cur = var
6+ from pythonbpf .expr import get_base_type_and_depth , deref_to_depth
157
16- while isinstance (cur .type , ir .PointerType ):
17- cur = builder .load (cur )
18- chain .append (cur )
19-
20- if isinstance (cur .type , ir .IntType ):
21- logger .info (f"dereference chain: { chain } " )
22- return cur , chain
23- else :
24- raise TypeError (f"Unsupported type for dereferencing: { cur .type } " )
8+ logger : Logger = logging .getLogger (__name__ )
259
2610
27- def get_operand_value (operand , builder , local_sym_tab ):
11+ def get_operand_value (func , operand , builder , local_sym_tab ):
2812 """Extract the value from an operand, handling variables and constants."""
2913 if isinstance (operand , ast .Name ):
3014 if operand .id in local_sym_tab :
3115 var = local_sym_tab [operand .id ].var
32- val , chain = deref_to_val (var , builder )
33- return val , chain , var
16+ var_type = var .type
17+ base_type , depth = get_base_type_and_depth (var_type )
18+ logger .info (f"var is { var } , base_type is { base_type } , depth is { depth } " )
19+ val = deref_to_depth (func , builder , var , depth )
20+ return val , [val ], var
3421 raise ValueError (f"Undefined variable: { operand .id } " )
3522 elif isinstance (operand , ast .Constant ):
3623 if isinstance (operand .value , int ):
3724 cst = ir .Constant (ir .IntType (64 ), operand .value )
3825 return cst , [cst ], None
3926 raise TypeError (f"Unsupported constant type: { type (operand .value )} " )
4027 elif isinstance (operand , ast .BinOp ):
41- res = handle_binary_op_impl (operand , builder , local_sym_tab )
28+ res = handle_binary_op_impl (func , operand , builder , local_sym_tab )
4229 return res , [res ], None
4330 raise TypeError (f"Unsupported operand type: { type (operand )} " )
4431
@@ -53,10 +40,10 @@ def store_through_chain(value, chain, builder):
5340 value = ptr
5441
5542
56- def handle_binary_op_impl (rval , builder , local_sym_tab ):
43+ def handle_binary_op_impl (func , rval , builder , local_sym_tab ):
5744 op = rval .op
58- left , lchain , _ = get_operand_value (rval .left , builder , local_sym_tab )
59- right , rchain , _ = get_operand_value (rval .right , builder , local_sym_tab )
45+ left , lchain , _ = get_operand_value (func , rval .left , builder , local_sym_tab )
46+ right , rchain , _ = get_operand_value (func , rval .right , builder , local_sym_tab )
6047 logger .info (f"left is { left } , right is { right } , op is { op } " )
6148
6249 logger .info (f"left chain: { lchain } , right chain: { rchain } " )
@@ -83,8 +70,8 @@ def handle_binary_op_impl(rval, builder, local_sym_tab):
8370 raise SyntaxError ("Unsupported binary operation" )
8471
8572
86- def handle_binary_op (rval , builder , var_name , local_sym_tab ):
87- result = handle_binary_op_impl (rval , builder , local_sym_tab )
73+ def handle_binary_op (func , rval , builder , var_name , local_sym_tab ):
74+ result = handle_binary_op_impl (func , rval , builder , local_sym_tab )
8875 if var_name and var_name in local_sym_tab :
8976 logger .info (
9077 f"Storing result { result } into variable { local_sym_tab [var_name ].var } "
0 commit comments