Skip to content

Commit 45e6ce5

Browse files
committed
Move deref_to_depth to expr/ir_ops.py
1 parent c5f0a28 commit 45e6ce5

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

pythonbpf/expr/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .expr_pass import eval_expr, handle_expr, get_operand_value, CallHandlerRegistry
2-
from .type_normalization import convert_to_bool, get_base_type_and_depth, deref_to_depth
2+
from .type_normalization import convert_to_bool, get_base_type_and_depth
3+
from .ir_ops import deref_to_depth
34

45
__all__ = [
56
"eval_expr",

pythonbpf/expr/ir_ops.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import logging
2+
from llvmlite import ir
3+
4+
logger = logging.getLogger(__name__)
5+
6+
7+
def deref_to_depth(func, builder, val, target_depth):
8+
"""Dereference a pointer to a certain depth."""
9+
10+
cur_val = val
11+
cur_type = val.type
12+
13+
for depth in range(target_depth):
14+
if not isinstance(val.type, ir.PointerType):
15+
logger.error("Cannot dereference further, non-pointer type")
16+
return None
17+
18+
# dereference with null check
19+
pointee_type = cur_type.pointee
20+
null_check_block = builder.block
21+
not_null_block = func.append_basic_block(name=f"deref_not_null_{depth}")
22+
merge_block = func.append_basic_block(name=f"deref_merge_{depth}")
23+
24+
null_ptr = ir.Constant(cur_type, None)
25+
is_not_null = builder.icmp_signed("!=", cur_val, null_ptr)
26+
logger.debug(f"Inserted null check for pointer at depth {depth}")
27+
28+
builder.cbranch(is_not_null, not_null_block, merge_block)
29+
30+
builder.position_at_end(not_null_block)
31+
dereferenced_val = builder.load(cur_val)
32+
logger.debug(f"Dereferenced to depth {depth - 1}, type: {pointee_type}")
33+
builder.branch(merge_block)
34+
35+
builder.position_at_end(merge_block)
36+
phi = builder.phi(pointee_type, name=f"deref_result_{depth}")
37+
38+
zero_value = (
39+
ir.Constant(pointee_type, 0)
40+
if isinstance(pointee_type, ir.IntType)
41+
else ir.Constant(pointee_type, None)
42+
)
43+
phi.add_incoming(zero_value, null_check_block)
44+
45+
phi.add_incoming(dereferenced_val, not_null_block)
46+
47+
# Continue with phi result
48+
cur_val = phi
49+
cur_type = pointee_type
50+
return cur_val

pythonbpf/expr/type_normalization.py

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from llvmlite import ir
21
import logging
32
import ast
3+
from llvmlite import ir
4+
from .ir_ops import deref_to_depth
45

56
logger = logging.getLogger(__name__)
67

@@ -26,52 +27,6 @@ def get_base_type_and_depth(ir_type):
2627
return cur_type, depth
2728

2829

29-
def deref_to_depth(func, builder, val, target_depth):
30-
"""Dereference a pointer to a certain depth."""
31-
32-
cur_val = val
33-
cur_type = val.type
34-
35-
for depth in range(target_depth):
36-
if not isinstance(val.type, ir.PointerType):
37-
logger.error("Cannot dereference further, non-pointer type")
38-
return None
39-
40-
# dereference with null check
41-
pointee_type = cur_type.pointee
42-
null_check_block = builder.block
43-
not_null_block = func.append_basic_block(name=f"deref_not_null_{depth}")
44-
merge_block = func.append_basic_block(name=f"deref_merge_{depth}")
45-
46-
null_ptr = ir.Constant(cur_type, None)
47-
is_not_null = builder.icmp_signed("!=", cur_val, null_ptr)
48-
logger.debug(f"Inserted null check for pointer at depth {depth}")
49-
50-
builder.cbranch(is_not_null, not_null_block, merge_block)
51-
52-
builder.position_at_end(not_null_block)
53-
dereferenced_val = builder.load(cur_val)
54-
logger.debug(f"Dereferenced to depth {depth - 1}, type: {pointee_type}")
55-
builder.branch(merge_block)
56-
57-
builder.position_at_end(merge_block)
58-
phi = builder.phi(pointee_type, name=f"deref_result_{depth}")
59-
60-
zero_value = (
61-
ir.Constant(pointee_type, 0)
62-
if isinstance(pointee_type, ir.IntType)
63-
else ir.Constant(pointee_type, None)
64-
)
65-
phi.add_incoming(zero_value, null_check_block)
66-
67-
phi.add_incoming(dereferenced_val, not_null_block)
68-
69-
# Continue with phi result
70-
cur_val = phi
71-
cur_type = pointee_type
72-
return cur_val
73-
74-
7530
def _normalize_types(func, builder, lhs, rhs):
7631
"""Normalize types for comparison."""
7732

0 commit comments

Comments
 (0)