Skip to content

Commit 6fea580

Browse files
committed
Fix t/f/return.py, tweak handle_binary_ops
1 parent b351346 commit 6fea580

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

pythonbpf/binary_ops.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ def handle_binary_op_impl(rval, module, builder, local_sym_tab):
6363

6464
def handle_binary_op(rval, module, builder, var_name, local_sym_tab):
6565
result = handle_binary_op_impl(rval, module, builder, local_sym_tab)
66-
builder.store(result, local_sym_tab[var_name].var)
66+
if var_name in local_sym_tab:
67+
builder.store(result, local_sym_tab[var_name].var)
68+
return result, result.type

pythonbpf/codegen.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def processor(source_code, filename, module):
4848
globals_processing(tree, module)
4949

5050

51-
def compile_to_ir(filename: str, output: str, loglevel=logging.WARNING):
51+
def compile_to_ir(filename: str, output: str, loglevel=logging.INFO):
5252
logging.basicConfig(
5353
level=loglevel, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
5454
)
@@ -121,7 +121,7 @@ def compile_to_ir(filename: str, output: str, loglevel=logging.WARNING):
121121
return output
122122

123123

124-
def compile(loglevel=logging.WARNING) -> bool:
124+
def compile(loglevel=logging.INFO) -> bool:
125125
# Look one level up the stack to the caller of this function
126126
caller_frame = inspect.stack()[1]
127127
caller_file = Path(caller_frame.filename).resolve()
@@ -154,7 +154,7 @@ def compile(loglevel=logging.WARNING) -> bool:
154154
return success
155155

156156

157-
def BPF(loglevel=logging.WARNING) -> BpfProgram:
157+
def BPF(loglevel=logging.INFO) -> BpfProgram:
158158
caller_frame = inspect.stack()[1]
159159
src = inspect.getsource(caller_frame.frame)
160160
with tempfile.NamedTemporaryFile(

pythonbpf/functions_pass.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,31 @@ def process_stmt(
391391
isinstance(stmt.value, ast.Call)
392392
and isinstance(stmt.value.func, ast.Name)
393393
and len(stmt.value.args) == 1
394-
and isinstance(stmt.value.args[0], ast.Constant)
395-
and isinstance(stmt.value.args[0].value, int)
396394
):
397-
call_type = stmt.value.func.id
398-
if ctypes_to_ir(call_type) != ret_type:
399-
raise ValueError(
400-
"Return type mismatch: expected"
401-
f"{ctypes_to_ir(call_type)}, got {call_type}"
395+
if isinstance(stmt.value.args[0], ast.Constant) and isinstance(
396+
stmt.value.args[0].value, int
397+
):
398+
call_type = stmt.value.func.id
399+
if ctypes_to_ir(call_type) != ret_type:
400+
raise ValueError(
401+
"Return type mismatch: expected"
402+
f"{ctypes_to_ir(call_type)}, got {call_type}"
403+
)
404+
else:
405+
builder.ret(ir.Constant(ret_type, stmt.value.args[0].value))
406+
did_return = True
407+
elif isinstance(stmt.value.args[0], ast.BinOp):
408+
# TODO: Should be routed through eval_expr
409+
val = handle_binary_op(
410+
stmt.value.args[0], module, builder, None, local_sym_tab
402411
)
403-
else:
404-
builder.ret(ir.Constant(ret_type, stmt.value.args[0].value))
412+
if val is None:
413+
raise ValueError("Failed to evaluate return expression")
414+
if val[1] != ret_type:
415+
raise ValueError(
416+
"Return type mismatch: expected" f"{ret_type}, got {val[1]}"
417+
)
418+
builder.ret(val[0])
405419
did_return = True
406420
elif isinstance(stmt.value, ast.Name):
407421
if stmt.value.id == "XDP_PASS":

tests/failing_tests/var_rval.py

Whitespace-only changes.
File renamed without changes.

0 commit comments

Comments
 (0)