|
1 | 1 | import ast |
| 2 | +import logging |
2 | 3 | from llvmlite import ir |
3 | 4 |
|
| 5 | +logger = logging.getLogger(__name__) |
| 6 | + |
4 | 7 |
|
5 | 8 | class HelperHandlerRegistry: |
6 | 9 | """Registry for BPF helpers""" |
@@ -67,3 +70,48 @@ def get_flags_val(arg, builder, local_sym_tab): |
67 | 70 |
|
68 | 71 | raise NotImplementedError( |
69 | 72 | "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