|
3 | 3 | from inspect import isclass |
4 | 4 |
|
5 | 5 | from llvmlite import ir |
6 | | -from pythonbpf.expr import eval_expr |
| 6 | +from pythonbpf.expr import eval_expr, convert |
7 | 7 | from pythonbpf.helper import emit_probe_read_kernel_str_call |
8 | 8 | from pythonbpf.type_deducer import ctypes_to_ir |
9 | 9 | from pythonbpf.vmlinux_parser.dependency_node import Field |
@@ -57,10 +57,7 @@ def handle_struct_field_assignment( |
57 | 57 | # Same implicit widening/truncation as assignment to a local: expressions |
58 | 58 | # evaluate in i64, but a field may be narrower. |
59 | 59 | if isinstance(val_type, ir.IntType) and isinstance(field_type, ir.IntType): |
60 | | - if val_type.width < field_type.width: |
61 | | - val = builder.sext(val, field_type) |
62 | | - elif val_type.width > field_type.width: |
63 | | - val = builder.trunc(val, field_type) |
| 60 | + val = convert(builder, val, val_type, field_type) |
64 | 61 |
|
65 | 62 | # Regular assignment |
66 | 63 | builder.store(val, field_ptr) |
@@ -220,13 +217,7 @@ def handle_variable_assignment( |
220 | 217 | ) |
221 | 218 | return False |
222 | 219 | elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.IntType): |
223 | | - # Allow implicit int widening |
224 | | - if val_type.width < var_type.width: |
225 | | - val = builder.sext(val, var_type) |
226 | | - logger.info(f"Implicitly widened int for variable {var_name}") |
227 | | - elif val_type.width > var_type.width: |
228 | | - val = builder.trunc(val, var_type) |
229 | | - logger.info(f"Implicitly truncated int for variable {var_name}") |
| 220 | + val = convert(builder, val, val_type, var_type) |
230 | 221 | elif isinstance(val_type, ir.IntType) and isinstance(var_type, ir.PointerType): |
231 | 222 | # NOTE: This is assignment to a PTR_TO_MAP_VALUE_OR_NULL |
232 | 223 | logger.info( |
|
0 commit comments