Skip to content

Commit dadcb69

Browse files
committed
Store LocalSymbol in allocate_mem
1 parent 2fd2a46 commit dadcb69

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

pythonbpf/functions_pass.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,16 @@ def handle_assign(
9393
elif isinstance(rval, ast.Constant):
9494
if isinstance(rval.value, bool):
9595
if rval.value:
96-
builder.store(ir.Constant(ir.IntType(1), 1),
97-
local_sym_tab[var_name][0])
96+
builder.store(ir.Constant(ir.IntType(1), 1), local_sym_tab[var_name][0])
9897
else:
99-
builder.store(ir.Constant(ir.IntType(1), 0),
100-
local_sym_tab[var_name][0])
98+
builder.store(ir.Constant(ir.IntType(1), 0), local_sym_tab[var_name][0])
10199
print(f"Assigned constant {rval.value} to {var_name}")
102100
elif isinstance(rval.value, int):
103101
# Assume c_int64 for now
104102
# var = builder.alloca(ir.IntType(64), name=var_name)
105103
# var.align = 8
106104
builder.store(
107-
ir.Constant(ir.IntType(64),
108-
rval.value), local_sym_tab[var_name][0]
105+
ir.Constant(ir.IntType(64), rval.value), local_sym_tab[var_name][0]
109106
)
110107
# local_sym_tab[var_name] = var
111108
print(f"Assigned constant {rval.value} to {var_name}")
@@ -120,8 +117,7 @@ def handle_assign(
120117
global_str.linkage = "internal"
121118
global_str.global_constant = True
122119
global_str.initializer = str_const
123-
str_ptr = builder.bitcast(
124-
global_str, ir.PointerType(ir.IntType(8)))
120+
str_ptr = builder.bitcast(global_str, ir.PointerType(ir.IntType(8)))
125121
builder.store(str_ptr, local_sym_tab[var_name][0])
126122
print(f"Assigned string constant '{rval.value}' to {var_name}")
127123
else:
@@ -140,8 +136,7 @@ def handle_assign(
140136
# var = builder.alloca(ir_type, name=var_name)
141137
# var.align = ir_type.width // 8
142138
builder.store(
143-
ir.Constant(
144-
ir_type, rval.args[0].value), local_sym_tab[var_name][0]
139+
ir.Constant(ir_type, rval.args[0].value), local_sym_tab[var_name][0]
145140
)
146141
print(
147142
f"Assigned {call_type} constant "
@@ -187,8 +182,7 @@ def handle_assign(
187182
ir_type = struct_info.ir_type
188183
# var = builder.alloca(ir_type, name=var_name)
189184
# Null init
190-
builder.store(ir.Constant(ir_type, None),
191-
local_sym_tab[var_name][0])
185+
builder.store(ir.Constant(ir_type, None), local_sym_tab[var_name][0])
192186
local_var_metadata[var_name] = call_type
193187
print(f"Assigned struct {call_type} to {var_name}")
194188
# local_sym_tab[var_name] = var
@@ -259,8 +253,7 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
259253
print(f"Undefined variable {cond.id} in condition")
260254
return None
261255
elif isinstance(cond, ast.Compare):
262-
lhs = eval_expr(func, module, builder, cond.left,
263-
local_sym_tab, map_sym_tab)[0]
256+
lhs = eval_expr(func, module, builder, cond.left, local_sym_tab, map_sym_tab)[0]
264257
if len(cond.ops) != 1 or len(cond.comparators) != 1:
265258
print("Unsupported complex comparison")
266259
return None
@@ -313,8 +306,7 @@ def handle_if(
313306
else:
314307
else_block = None
315308

316-
cond = handle_cond(func, module, builder, stmt.test,
317-
local_sym_tab, map_sym_tab)
309+
cond = handle_cond(func, module, builder, stmt.test, local_sym_tab, map_sym_tab)
318310
if else_block:
319311
builder.cbranch(cond, then_block, else_block)
320312
else:
@@ -419,6 +411,7 @@ def allocate_mem(
419411
module, builder, body, func, ret_type, map_sym_tab, local_sym_tab, structs_sym_tab
420412
):
421413
for stmt in body:
414+
has_metadata = False
422415
if isinstance(stmt, ast.If):
423416
if stmt.body:
424417
local_sym_tab = allocate_mem(
@@ -459,8 +452,7 @@ def allocate_mem(
459452
ir_type = ctypes_to_ir(call_type)
460453
var = builder.alloca(ir_type, name=var_name)
461454
var.align = ir_type.width // 8
462-
print(
463-
f"Pre-allocated variable {var_name} of type {call_type}")
455+
print(f"Pre-allocated variable {var_name} of type {call_type}")
464456
elif HelperHandlerRegistry.has_handler(call_type):
465457
# Assume return type is int64 for now
466458
ir_type = ir.IntType(64)
@@ -477,7 +469,7 @@ def allocate_mem(
477469
struct_info = structs_sym_tab[call_type]
478470
ir_type = struct_info.ir_type
479471
var = builder.alloca(ir_type, name=var_name)
480-
local_var_metadata[var_name] = call_type
472+
has_metadata = True
481473
print(
482474
f"Pre-allocated variable {var_name} "
483475
f"for struct {call_type}"
@@ -519,7 +511,11 @@ def allocate_mem(
519511
else:
520512
print("Unsupported assignment value type")
521513
continue
522-
local_sym_tab[var_name] = (var, ir_type)
514+
515+
if has_metadata:
516+
local_sym_tab[var_name] = LocalSymbol(var, ir_type, call_type)
517+
else:
518+
local_sym_tab[var_name] = LocalSymbol(var, ir_type)
523519
return local_sym_tab
524520

525521

@@ -681,8 +677,7 @@ def _expr_type(e):
681677
if found_type is None:
682678
found_type = t
683679
elif found_type != t:
684-
raise ValueError("Conflicting return types:" f"{
685-
found_type} vs {t}")
680+
raise ValueError(f"Conflicting return types:{found_type} vs {t}")
686681
return found_type or "None"
687682

688683

@@ -719,8 +714,7 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l
719714
char = builder.load(src_ptr)
720715

721716
# Store character in target
722-
dst_ptr = builder.gep(
723-
target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx])
717+
dst_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), idx])
724718
builder.store(char, dst_ptr)
725719

726720
# Increment counter
@@ -731,6 +725,5 @@ def assign_string_to_array(builder, target_array_ptr, source_string_ptr, array_l
731725

732726
# Ensure null termination
733727
last_idx = ir.Constant(ir.IntType(32), array_length - 1)
734-
null_ptr = builder.gep(
735-
target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx])
728+
null_ptr = builder.gep(target_array_ptr, [ir.Constant(ir.IntType(32), 0), last_idx])
736729
builder.store(ir.Constant(ir.IntType(8), 0), null_ptr)

0 commit comments

Comments
 (0)