Skip to content

Commit fed4c17

Browse files
committed
fix struct usage in functions_pass
1 parent 32c22c3 commit fed4c17

1 file changed

Lines changed: 25 additions & 24 deletions

File tree

pythonbpf/functions_pass.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,17 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc
4949
struct_type = local_var_metadata[var_name]
5050
struct_info = structs_sym_tab[struct_type]
5151

52-
if field_name in struct_info["fields"]:
53-
field_idx = struct_info["fields"][field_name]
54-
struct_ptr = local_sym_tab[var_name][0]
55-
field_ptr = builder.gep(
56-
struct_ptr, [ir.Constant(ir.IntType(32), 0),
57-
ir.Constant(ir.IntType(32), field_idx)],
58-
inbounds=True)
52+
if field_name in struct_info.fields:
53+
field_ptr = struct_info.gep(
54+
builder, local_sym_tab[var_name][0], field_name)
5955
val = eval_expr(func, module, builder, rval,
6056
local_sym_tab, map_sym_tab, structs_sym_tab)
61-
if isinstance(struct_info["field_types"][field_idx], ir.ArrayType) and val[1] == ir.PointerType(ir.IntType(8)):
57+
if isinstance(struct_info.field_type(field_name), ir.ArrayType) and val[1] == ir.PointerType(ir.IntType(8)):
6258
# TODO: Figure it out, not a priority rn
6359
# Special case for string assignment to char array
64-
#str_len = struct_info["field_types"][field_idx].count
65-
#assign_string_to_array(builder, field_ptr, val[0], str_len)
66-
#print(f"Assigned to struct field {var_name}.{field_name}")
60+
# str_len = struct_info["field_types"][field_idx].count
61+
# assign_string_to_array(builder, field_ptr, val[0], str_len)
62+
# print(f"Assigned to struct field {var_name}.{field_name}")
6763
pass
6864
if val is None:
6965
print("Failed to evaluate struct field assignment")
@@ -138,7 +134,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab, struc
138134
print(f"Dereferenced and assigned to {var_name}")
139135
elif call_type in structs_sym_tab and len(rval.args) == 0:
140136
struct_info = structs_sym_tab[call_type]
141-
ir_type = struct_info["type"]
137+
ir_type = struct_info.ir_type
142138
# var = builder.alloca(ir_type, name=var_name)
143139
# Null init
144140
builder.store(ir.Constant(ir_type, None),
@@ -364,7 +360,7 @@ def allocate_mem(module, builder, body, func, ret_type, map_sym_tab, local_sym_t
364360
f"Pre-allocated variable {var_name} for deref")
365361
elif call_type in structs_sym_tab:
366362
struct_info = structs_sym_tab[call_type]
367-
ir_type = struct_info["type"]
363+
ir_type = struct_info.ir_type
368364
var = builder.alloca(ir_type, name=var_name)
369365
local_var_metadata[var_name] = call_type
370366
print(
@@ -548,6 +544,8 @@ def _expr_type(e):
548544
return found_type or "None"
549545

550546
# For string assignment to fixed-size arrays
547+
548+
551549
def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_length):
552550
"""
553551
Copy a string (i8*) to a fixed-size array ([N x i8]*)
@@ -556,36 +554,39 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l
556554
entry_block = builder.block
557555
copy_block = builder.append_basic_block("copy_char")
558556
end_block = builder.append_basic_block("copy_end")
559-
557+
560558
# Create loop counter
561559
i = builder.alloca(ir.IntType(32))
562560
builder.store(ir.Constant(ir.IntType(32), 0), i)
563-
561+
564562
# Start the loop
565563
builder.branch(copy_block)
566-
564+
567565
# Copy loop
568566
builder.position_at_end(copy_block)
569567
idx = builder.load(i)
570-
in_bounds = builder.icmp_unsigned('<', idx, ir.Constant(ir.IntType(32), array_length))
568+
in_bounds = builder.icmp_unsigned(
569+
'<', idx, ir.Constant(ir.IntType(32), array_length))
571570
builder.cbranch(in_bounds, copy_block, end_block)
572-
571+
573572
with builder.if_then(in_bounds):
574573
# Load character from source
575574
src_ptr = builder.gep(source_string_ptr, [idx])
576575
char = builder.load(src_ptr)
577-
576+
578577
# Store character in target
579-
dst_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx])
578+
dst_ptr = builder.gep(
579+
target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx])
580580
builder.store(char, dst_ptr)
581-
581+
582582
# Increment counter
583583
next_idx = builder.add(idx, ir.Constant(ir.IntType(32), 1))
584584
builder.store(next_idx, i)
585-
585+
586586
builder.position_at_end(end_block)
587-
587+
588588
# Ensure null termination
589589
last_idx = ir.Constant(ir.IntType(32), array_length - 1)
590-
null_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx])
590+
null_ptr = builder.gep(
591+
target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx])
591592
builder.store(ir.Constant(ir.IntType(8), 0), null_ptr)

0 commit comments

Comments
 (0)