Skip to content

Commit d18c69f

Browse files
committed
Add _handle_fstring_print scaffolding
1 parent 9c58116 commit d18c69f

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

pythonbpf/helper/helper_utils.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import ast
2+
import logging
23
from llvmlite import ir
34

5+
logger = logging.getLogger(__name__)
6+
47

58
class HelperHandlerRegistry:
69
"""Registry for BPF helpers"""
@@ -67,3 +70,48 @@ def get_flags_val(arg, builder, local_sym_tab):
6770

6871
raise NotImplementedError(
6972
"Only simple variable names or integer constants are supported as flags in map helpers.")
73+
74+
75+
def _handle_fstring_print(joined_str, module, builder, func,
76+
local_sym_tab=None, struct_sym_tab=None,
77+
local_var_metadata=None):
78+
"""Handle f-string formatting for bpf_printk emitter."""
79+
fmt_parts = []
80+
exprs = []
81+
82+
for value in joined_str.values:
83+
logger.debug(f"Processing f-string value: {ast.dump(value)}")
84+
85+
if isinstance(value, ast.Constant):
86+
_process_constant_in_fstring(value, fmt_parts, exprs)
87+
elif isinstance(value, ast.FormattedValue):
88+
_process_formatted_value(value, fmt_parts, exprs,
89+
local_sym_tab, struct_sym_tab,
90+
local_var_metadata)
91+
92+
93+
def _process_constant_in_fstring(cst, fmt_parts, exprs):
94+
"""Process constant values in f-string."""
95+
if isinstance(cst.value, str):
96+
fmt_parts.append(cst.value)
97+
elif isinstance(cst.value, int):
98+
fmt_parts.append("%lld")
99+
exprs.append(ir.Constant(ir.IntType(64), cst.value))
100+
else:
101+
raise NotImplementedError(
102+
f"Unsupported constant type in f-string: {type(cst.value)}")
103+
104+
105+
def _process_formatted_value(fval, fmt_parts, exprs,
106+
local_sym_tab, struct_sym_tab,
107+
local_var_metadata):
108+
"""Process formatted values in f-string."""
109+
logger.debug(f"Processing formatted value: {ast.dump(fval)}")
110+
111+
if isinstance(fval.value, ast.Name):
112+
pass
113+
elif isinstance(fval.value, ast.Attribute):
114+
pass
115+
else:
116+
raise NotImplementedError(
117+
f"Unsupported formatted value type in f-string: {type(fval.value)}")

0 commit comments

Comments
 (0)