add jupyter notebook support

This commit is contained in:
2025-09-27 12:23:29 +05:30
parent de5cc438ab
commit ea5a1ab2de
5 changed files with 415 additions and 14 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
__pycache__/ __pycache__/
*.ll *.ll
*.o *.o
.ipynb_checkpoints/

397
demo/clone-matplotlib.ipynb Normal file

File diff suppressed because one or more lines are too long

View File

@ -151,12 +151,12 @@ def bpf_printk_emitter(call, map_ptr, module, builder, func, local_sym_tab=None,
fmt_gvar = ir.GlobalVariable( fmt_gvar = ir.GlobalVariable(
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=fmt_name) module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=fmt_name)
fmt_gvar.global_constant = True fmt_gvar.global_constant = True
fmt_gvar.initializer = ir.Constant( fmt_gvar.initializer = ir.Constant( # type: ignore
ir.ArrayType(ir.IntType(8), len(fmt_str)), ir.ArrayType(ir.IntType(8), len(fmt_str)),
bytearray(fmt_str.encode("utf8")) bytearray(fmt_str.encode("utf8"))
) )
fmt_gvar.linkage = "internal" fmt_gvar.linkage = "internal"
fmt_gvar.align = 1 fmt_gvar.align = 1 # type: ignore
fmt_ptr = builder.bitcast(fmt_gvar, ir.PointerType()) fmt_ptr = builder.bitcast(fmt_gvar, ir.PointerType())

View File

@ -10,6 +10,7 @@ import subprocess
import inspect import inspect
from pathlib import Path from pathlib import Path
from pylibbpf import BpfProgram from pylibbpf import BpfProgram
import tempfile
def find_bpf_chunks(tree): def find_bpf_chunks(tree):
@ -122,14 +123,17 @@ def compile():
def BPF() -> BpfProgram: def BPF() -> BpfProgram:
caller_frame = inspect.stack()[1] caller_frame = inspect.stack()[1]
caller_file = Path(caller_frame.filename).resolve() src = inspect.getsource(caller_frame.frame)
ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name with tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".py") as f, \
o_file = Path("/tmp") / caller_file.with_suffix(".o").name tempfile.NamedTemporaryFile(mode="w+", delete=True, suffix=".ll") as inter, \
compile_to_ir(str(caller_file), str(ll_file)) tempfile.NamedTemporaryFile(mode="w+", delete=False, suffix=".o") as obj_file:
f.write(src)
f.flush()
source = f.name
compile_to_ir(source, str(inter.name))
subprocess.run([
"llc", "-march=bpf", "-filetype=obj", "-O2",
str(inter.name), "-o", str(obj_file.name)
], check=True)
subprocess.run([ return BpfProgram(str(obj_file.name))
"llc", "-march=bpf", "-filetype=obj", "-O2",
str(ll_file), "-o", str(o_file)
], check=True)
return BpfProgram(str(o_file))

View File

@ -1,7 +1,6 @@
import ast import ast
from llvmlite import ir from llvmlite import ir
from .type_deducer import ctypes_to_ir from .type_deducer import ctypes_to_ir
from . import dwarf_constants as dc
structs_sym_tab = {} structs_sym_tab = {}