Skip to content

Commit 4177a6c

Browse files
committed
Move eval_expr logic to cleanup handle_expr
1 parent 4f726a7 commit 4177a6c

File tree

1 file changed

+25
-38
lines changed

1 file changed

+25
-38
lines changed

pythonbpf/functions_pass.py

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
9999
print("Unsupported assignment value type")
100100

101101

102-
def handle_expr(func, module, builder, expr, local_sym_tab, map_sym_tab):
103-
"""Handle expression statements in the function body."""
104-
print(f"Handling expression: {ast.dump(expr)}")
105-
102+
def eval_expr(func, module, builder, expr, local_sym_tab, map_sym_tab):
106103
if isinstance(expr, ast.Name):
107104
if expr.id in local_sym_tab:
108105
var = local_sym_tab[expr.id]
@@ -119,40 +116,30 @@ def handle_expr(func, module, builder, expr, local_sym_tab, map_sym_tab):
119116
else:
120117
print("Unsupported constant type")
121118
return None
119+
elif isinstance(expr, ast.Call):
120+
if isinstance(expr.func, ast.Name):
121+
# check for helpers first
122+
if expr.func.id in helper_func_list:
123+
return handle_helper_call(
124+
expr, module, builder, func, local_sym_tab, map_sym_tab)
125+
elif isinstance(expr.func, ast.Attribute):
126+
if isinstance(expr.func.value, ast.Call) and isinstance(expr.func.value.func, ast.Name):
127+
method_name = expr.func.attr
128+
if method_name in helper_func_list:
129+
return handle_helper_call(
130+
expr, module, builder, func, local_sym_tab, map_sym_tab)
131+
print("Unsupported expression evaluation")
132+
return None
133+
122134

135+
def handle_expr(func, module, builder, expr, local_sym_tab, map_sym_tab):
136+
"""Handle expression statements in the function body."""
137+
print(f"Handling expression: {ast.dump(expr)}")
123138
call = expr.value
124139
if isinstance(call, ast.Call):
125-
if isinstance(call.func, ast.Name):
126-
# check for helpers first
127-
if call.func.id in helper_func_list:
128-
handle_helper_call(
129-
call, module, builder, func, local_sym_tab, map_sym_tab)
130-
return
131-
elif isinstance(call.func, ast.Attribute):
132-
if isinstance(call.func.value, ast.Call) and isinstance(call.func.value.func, ast.Name):
133-
method_name = call.func.attr
134-
if method_name in helper_func_list:
135-
handle_helper_call(
136-
call, module, builder, func, local_sym_tab, map_sym_tab)
137-
return
138-
elif isinstance(call, ast.Name):
139-
if call.id in local_sym_tab:
140-
var = local_sym_tab[call.id]
141-
val = builder.load(var)
142-
return val
143-
else:
144-
print(f"Undefined variable {call.id}")
145-
return None
146-
elif isinstance(call, ast.Constant):
147-
if isinstance(call.value, int):
148-
return ir.Constant(ir.IntType(64), call.value)
149-
elif isinstance(call.value, bool):
150-
return ir.Constant(ir.IntType(1), int(call.value))
151-
else:
152-
print("Unsupported constant type")
153-
return None
140+
eval_expr(func, module, builder, call, local_sym_tab, map_sym_tab)
154141
else:
155-
print("Unsupported expression statement")
142+
print("Unsupported expression type")
156143

157144

158145
def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
@@ -173,13 +160,13 @@ def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
173160
print(f"Undefined variable {cond.id} in condition")
174161
return None
175162
elif isinstance(cond, ast.Compare):
176-
lhs = handle_expr(func, module, builder, cond.left,
177-
local_sym_tab, map_sym_tab)
163+
lhs = eval_expr(func, module, builder, cond.left,
164+
local_sym_tab, map_sym_tab)
178165
if len(cond.ops) != 1 or len(cond.comparators) != 1:
179166
print("Unsupported complex comparison")
180167
return None
181-
rhs = handle_expr(func, module, builder,
182-
cond.comparators[0], local_sym_tab, map_sym_tab)
168+
rhs = eval_expr(func, module, builder,
169+
cond.comparators[0], local_sym_tab, map_sym_tab)
183170
op = cond.ops[0]
184171

185172
if lhs.type != rhs.type:

0 commit comments

Comments
 (0)