Skip to content

Commit 3a819dc

Browse files
committed
Add _handle_constant_expr
1 parent 729270b commit 3a819dc

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

pythonbpf/expr_pass.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ def _handle_name_expr(expr: ast.Name, local_sym_tab: Dict, builder: ir.IRBuilder
1818
return None
1919

2020

21+
def _handle_constant_expr(expr: ast.Constant):
22+
"""Handle ast.Constant expressions."""
23+
if isinstance(expr.value, int):
24+
return ir.Constant(ir.IntType(64), expr.value), ir.IntType(64)
25+
elif isinstance(expr.value, bool):
26+
return ir.Constant(ir.IntType(1), int(expr.value)), ir.IntType(1)
27+
else:
28+
logger.info("Unsupported constant type")
29+
return None
30+
31+
2132
def eval_expr(
2233
func,
2334
module,
@@ -31,13 +42,7 @@ def eval_expr(
3142
if isinstance(expr, ast.Name):
3243
return _handle_name_expr(expr, local_sym_tab, builder)
3344
elif isinstance(expr, ast.Constant):
34-
if isinstance(expr.value, int):
35-
return ir.Constant(ir.IntType(64), expr.value), ir.IntType(64)
36-
elif isinstance(expr.value, bool):
37-
return ir.Constant(ir.IntType(1), int(expr.value)), ir.IntType(1)
38-
else:
39-
logger.info("Unsupported constant type")
40-
return None
45+
return _handle_constant_expr(expr)
4146
elif isinstance(expr, ast.Call):
4247
# delayed import to avoid circular dependency
4348
from pythonbpf.helper import HelperHandlerRegistry, handle_helper_call
@@ -117,10 +122,8 @@ def eval_expr(
117122
attr_name = expr.attr
118123
if var_name in local_sym_tab:
119124
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
120-
logger.info(f"Loading attribute {
121-
attr_name} from variable {var_name}")
122-
logger.info(f"Variable type: {
123-
var_type}, Variable ptr: {var_ptr}")
125+
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
126+
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
124127
metadata = structs_sym_tab[var_metadata]
125128
if attr_name in metadata.fields:
126129
gep = metadata.gep(builder, var_ptr, attr_name)

0 commit comments

Comments
 (0)