Skip to content

Commit 9febadf

Browse files
committed
Add pointer handling to helper_utils, finish pointer assignment
1 parent 99aacca commit 9febadf

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

pythonbpf/expr/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
from .expr_pass import eval_expr, handle_expr
2-
from .type_normalization import convert_to_bool, get_base_type_and_depth
2+
from .type_normalization import convert_to_bool, get_base_type_and_depth, deref_to_depth
33

4-
__all__ = ["eval_expr", "handle_expr", "convert_to_bool", "get_base_type_and_depth"]
4+
__all__ = [
5+
"eval_expr",
6+
"handle_expr",
7+
"convert_to_bool",
8+
"get_base_type_and_depth",
9+
"deref_to_depth",
10+
]

pythonbpf/expr/expr_pass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def _handle_constant_expr(expr: ast.Constant):
2626
if isinstance(expr.value, int) or isinstance(expr.value, bool):
2727
return ir.Constant(ir.IntType(64), int(expr.value)), ir.IntType(64)
2828
else:
29-
logger.error("Unsupported constant type")
29+
logger.error(f"Unsupported constant type {ast.dump(expr)}")
3030
return None
3131

3232

pythonbpf/expr/type_normalization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_base_type_and_depth(ir_type):
2626
return cur_type, depth
2727

2828

29-
def _deref_to_depth(func, builder, val, target_depth):
29+
def deref_to_depth(func, builder, val, target_depth):
3030
"""Dereference a pointer to a certain depth."""
3131

3232
cur_val = val
@@ -92,9 +92,9 @@ def _normalize_types(func, builder, lhs, rhs):
9292
rhs_base, rhs_depth = get_base_type_and_depth(rhs.type)
9393
if lhs_base == rhs_base:
9494
if lhs_depth < rhs_depth:
95-
rhs = _deref_to_depth(func, builder, rhs, rhs_depth - lhs_depth)
95+
rhs = deref_to_depth(func, builder, rhs, rhs_depth - lhs_depth)
9696
elif rhs_depth < lhs_depth:
97-
lhs = _deref_to_depth(func, builder, lhs, lhs_depth - rhs_depth)
97+
lhs = deref_to_depth(func, builder, lhs, lhs_depth - rhs_depth)
9898
return _normalize_types(func, builder, lhs, rhs)
9999

100100

pythonbpf/helper/helper_utils.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from collections.abc import Callable
44

55
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
77

88
logger = logging.getLogger(__name__)
99

@@ -224,10 +224,27 @@ def _populate_fval(ftype, node, fmt_parts, exprs):
224224
raise NotImplementedError(
225225
f"Unsupported integer width in f-string: {ftype.width}"
226226
)
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+
)
231248
else:
232249
raise NotImplementedError(f"Unsupported field type in f-string: {ftype}")
233250

@@ -264,7 +281,20 @@ def _prepare_expr_args(expr, func, module, builder, local_sym_tab, struct_sym_ta
264281

265282
if val:
266283
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)
268298
elif isinstance(val.type, ir.IntType):
269299
if val.type.width < 64:
270300
val = builder.sext(val, ir.IntType(64))

0 commit comments

Comments
 (0)