mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
seperate expr handling logic to a different file to prevent circular import, add format strings
This commit is contained in:
@ -1,9 +1,11 @@
|
||||
from llvmlite import ir
|
||||
import ast
|
||||
|
||||
|
||||
from .bpf_helper_handler import helper_func_list, handle_helper_call
|
||||
from .type_deducer import ctypes_to_ir
|
||||
from .binary_ops import handle_binary_op
|
||||
from .expr_pass import eval_expr, handle_expr
|
||||
|
||||
|
||||
def get_probe_string(func_node):
|
||||
@ -22,6 +24,7 @@ def get_probe_string(func_node):
|
||||
return arg.value
|
||||
return "helper"
|
||||
|
||||
|
||||
def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
|
||||
"""Handle assignment statements in the function body."""
|
||||
if len(stmt.targets) != 1:
|
||||
@ -96,54 +99,12 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
|
||||
else:
|
||||
print("Unsupported assignment call function type")
|
||||
elif isinstance(rval, ast.BinOp):
|
||||
handle_binary_op(rval, module, builder, func, local_sym_tab, map_sym_tab)
|
||||
handle_binary_op(rval, module, builder, func,
|
||||
local_sym_tab, map_sym_tab)
|
||||
else:
|
||||
print("Unsupported assignment value type")
|
||||
|
||||
|
||||
def eval_expr(func, module, builder, expr, local_sym_tab, map_sym_tab):
|
||||
if isinstance(expr, ast.Name):
|
||||
if expr.id in local_sym_tab:
|
||||
var = local_sym_tab[expr.id]
|
||||
val = builder.load(var)
|
||||
return val
|
||||
else:
|
||||
print(f"Undefined variable {expr.id}")
|
||||
return None
|
||||
elif isinstance(expr, ast.Constant):
|
||||
if isinstance(expr.value, int):
|
||||
return ir.Constant(ir.IntType(64), expr.value)
|
||||
elif isinstance(expr.value, bool):
|
||||
return ir.Constant(ir.IntType(1), int(expr.value))
|
||||
else:
|
||||
print("Unsupported constant type")
|
||||
return None
|
||||
elif isinstance(expr, ast.Call):
|
||||
if isinstance(expr.func, ast.Name):
|
||||
# check for helpers first
|
||||
if expr.func.id in helper_func_list:
|
||||
return handle_helper_call(
|
||||
expr, module, builder, func, local_sym_tab, map_sym_tab)
|
||||
elif isinstance(expr.func, ast.Attribute):
|
||||
if isinstance(expr.func.value, ast.Call) and isinstance(expr.func.value.func, ast.Name):
|
||||
method_name = expr.func.attr
|
||||
if method_name in helper_func_list:
|
||||
return handle_helper_call(
|
||||
expr, module, builder, func, local_sym_tab, map_sym_tab)
|
||||
print("Unsupported expression evaluation")
|
||||
return None
|
||||
|
||||
|
||||
def handle_expr(func, module, builder, expr, local_sym_tab, map_sym_tab):
|
||||
"""Handle expression statements in the function body."""
|
||||
print(f"Handling expression: {ast.dump(expr)}")
|
||||
call = expr.value
|
||||
if isinstance(call, ast.Call):
|
||||
eval_expr(func, module, builder, call, local_sym_tab, map_sym_tab)
|
||||
else:
|
||||
print("Unsupported expression type")
|
||||
|
||||
|
||||
def handle_cond(func, module, builder, cond, local_sym_tab, map_sym_tab):
|
||||
if isinstance(cond, ast.Constant):
|
||||
if isinstance(cond.value, bool):
|
||||
|
||||
Reference in New Issue
Block a user