Skip to content

Commit 6f6f101

Browse files
Add support for basic arithmetic operations
1 parent 4177a6c commit 6f6f101

File tree

3 files changed

+84
-8
lines changed

3 files changed

+84
-8
lines changed

examples/execve3.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,23 @@ def hello_again(ctx: c_void_p) -> c_int64:
3939
print("we did not prevail")
4040
ts = ktime()
4141
last().update(key, ts)
42-
keys = 2
43-
last().update(keys, ts)
44-
key = 4
45-
last().update(key, ts)
46-
key = 5
42+
4743
st = "st"
4844
last().update(key, ts)
49-
return c_int64(0)
5045

46+
keena = 2 + 1
47+
# below breaks
48+
# keela = keena + 1
49+
keema = 8 * 9
50+
keesa = 10 - 11
51+
keeda = 10 / 5
52+
# below does not spit IR
53+
keeda += 1
54+
return c_int64(0)
5155

5256
@bpf
5357
@bpfglobal
5458
def LICENSE() -> str:
5559
return "GPL"
5660

57-
5861
compile()

pythonbpf/functions_pass.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from .bpf_helper_handler import helper_func_list, handle_helper_call
55
from .type_deducer import ctypes_to_ir
6+
from .unary_and_binary_ops import handle_binary_op, handle_unary_op
67

78

89
def get_probe_string(func_node):
@@ -21,6 +22,16 @@ def get_probe_string(func_node):
2122
return arg.value
2223
return "helper"
2324

25+
def handle_unary_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
26+
"""Handle unary assignment statements in the function body."""
27+
SyntaxError("Unary assignment not supported")
28+
target = stmt.target
29+
if not isinstance(target, ast.Name):
30+
SyntaxError("Unsupported assignment target")
31+
return
32+
else:
33+
handle_unary_op(func, module, builder, stmt, map_sym_tab, local_sym_tab)
34+
return
2435

2536
def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
2637
"""Handle assignment statements in the function body."""
@@ -95,6 +106,8 @@ def handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab):
95106
print("Unsupported assignment call structure")
96107
else:
97108
print("Unsupported assignment call function type")
109+
elif isinstance(rval, ast.BinOp):
110+
handle_binary_op(rval, module, builder, func, local_sym_tab, map_sym_tab)
98111
else:
99112
print("Unsupported assignment value type")
100113

@@ -237,11 +250,13 @@ def handle_if(func, module, builder, stmt, map_sym_tab, local_sym_tab):
237250

238251

239252
def process_stmt(func, module, builder, stmt, local_sym_tab, map_sym_tab, did_return, ret_type=ir.IntType(64)):
240-
# print(f"Processing statement: {ast.dump(stmt)}")
253+
print(f"Processing statement: {ast.dump(stmt)}")
241254
if isinstance(stmt, ast.Expr):
242255
handle_expr(func, module, builder, stmt, local_sym_tab, map_sym_tab)
243256
elif isinstance(stmt, ast.Assign):
244257
handle_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab)
258+
elif isinstance(stmt, ast.AugAssign):
259+
handle_unary_assign(func, module, builder, stmt, map_sym_tab, local_sym_tab)
245260
elif isinstance(stmt, ast.If):
246261
handle_if(func, module, builder, stmt, map_sym_tab, local_sym_tab)
247262
elif isinstance(stmt, ast.Return):

pythonbpf/unary_and_binary_ops.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import ast
2+
from llvmlite import ir
3+
4+
def handle_binary_op(rval, module, builder, func, local_sym_tab, map_sym_tab):
5+
left = rval.left
6+
right = rval.right
7+
op = rval.op
8+
9+
if isinstance(left, ast.Name):
10+
left = local_sym_tab[left.id]
11+
elif isinstance(left, ast.Constant):
12+
left = ir.Constant(ir.IntType(64), left.value)
13+
else:
14+
print("Unsupported left operand type")
15+
16+
if isinstance(right, ast.Name):
17+
right = local_sym_tab[right.id]
18+
elif isinstance(right, ast.Constant):
19+
right = ir.Constant(ir.IntType(64), right.value)
20+
else:
21+
SyntaxError("Unsupported right operand type")
22+
23+
if isinstance(op, ast.Add):
24+
result = builder.add(left, right)
25+
elif isinstance(op, ast.Sub):
26+
result = builder.sub(left, right)
27+
elif isinstance(op, ast.Mult):
28+
result = builder.mul(left, right)
29+
elif isinstance(op, ast.Div):
30+
result = builder.sdiv(left, right)
31+
else:
32+
result = "fuck type errors"
33+
SyntaxError("Unsupported binary operation")
34+
35+
return result
36+
37+
def handle_unary_op(rval, module, builder, func, local_sym_tab, map_sym_tab):
38+
print("UNARY ASSIGNMENT DOES NOT WORK")
39+
return
40+
# TODO: heavy fixxing needed
41+
operand = rval.gay
42+
op = rval.op
43+
if isinstance(operand, ast.Name):
44+
operand = local_sym_tab[operand.id]
45+
elif isinstance(operand, ast.Constant):
46+
operand = ir.Constant(ir.IntType(64), operand.value)
47+
else:
48+
SyntaxError("Unsupported operand type")
49+
50+
if isinstance(op, ast.UAdd):
51+
result = builder.add(ir.Constant(ir.IntType(64), 0), operand)
52+
elif isinstance(op, ast.USub):
53+
result = builder.sub(ir.Constant(ir.IntType(64), 0), operand)
54+
else:
55+
result = "all my homies hate type errors"
56+
SyntaxError("Unsupported unary operation")
57+
58+
return result

0 commit comments

Comments
 (0)