|
3 | 3 | from collections.abc import Callable |
4 | 4 |
|
5 | 5 | from llvmlite import ir |
6 | | -from pythonbpf.expr import eval_expr |
| 6 | +from pythonbpf.expr import eval_expr, get_base_type_and_depth, deref_to_depth |
7 | 7 |
|
8 | 8 | logger = logging.getLogger(__name__) |
9 | 9 |
|
@@ -224,10 +224,27 @@ def _populate_fval(ftype, node, fmt_parts, exprs): |
224 | 224 | raise NotImplementedError( |
225 | 225 | f"Unsupported integer width in f-string: {ftype.width}" |
226 | 226 | ) |
227 | | - elif ftype == ir.PointerType(ir.IntType(8)): |
228 | | - # NOTE: We assume i8* is a string |
229 | | - fmt_parts.append("%s") |
230 | | - exprs.append(node) |
| 227 | + elif isinstance(ftype, ir.PointerType): |
| 228 | + target, depth = get_base_type_and_depth(ftype) |
| 229 | + if isinstance(target, ir.IntType): |
| 230 | + if target.width == 64: |
| 231 | + fmt_parts.append("%lld") |
| 232 | + exprs.append(node) |
| 233 | + elif target.width == 32: |
| 234 | + fmt_parts.append("%d") |
| 235 | + exprs.append(node) |
| 236 | + elif target.width == 8 and depth == 1: |
| 237 | + # NOTE: Assume i8* is a string |
| 238 | + fmt_parts.append("%s") |
| 239 | + exprs.append(node) |
| 240 | + else: |
| 241 | + raise NotImplementedError( |
| 242 | + f"Unsupported pointer target type in f-string: {target}" |
| 243 | + ) |
| 244 | + else: |
| 245 | + raise NotImplementedError( |
| 246 | + f"Unsupported pointer target type in f-string: {target}" |
| 247 | + ) |
231 | 248 | else: |
232 | 249 | raise NotImplementedError(f"Unsupported field type in f-string: {ftype}") |
233 | 250 |
|
@@ -264,7 +281,20 @@ def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_ta |
264 | 281 |
|
265 | 282 | if val: |
266 | 283 | if isinstance(val.type, ir.PointerType): |
267 | | - val = builder.ptrtoint(val, ir.IntType(64)) |
| 284 | + target, depth = get_base_type_and_depth(val.type) |
| 285 | + if isinstance(target, ir.IntType): |
| 286 | + if target.width >= 32: |
| 287 | + val = deref_to_depth(func, builder, val, depth) |
| 288 | + val = builder.sext(val, ir.IntType(64)) |
| 289 | + elif target.width == 8 and depth == 1: |
| 290 | + # NOTE: i8* is string, no need to deref |
| 291 | + pass |
| 292 | + |
| 293 | + else: |
| 294 | + logger.warning( |
| 295 | + "Only int and ptr supported in bpf_printk args. Others default to 0." |
| 296 | + ) |
| 297 | + val = ir.Constant(ir.IntType(64), 0) |
268 | 298 | elif isinstance(val.type, ir.IntType): |
269 | 299 | if val.type.width < 64: |
270 | 300 | val = builder.sext(val, ir.IntType(64)) |
|
0 commit comments