Files
python-bpf/pythonbpf/codegen.py
Pragyansh Chaturvedi c9e4910a89 hardcoded func gen
2025-09-06 12:49:37 +05:30

41 lines
1.4 KiB
Python

import ast
from llvmlite import ir
from .license_pass import license_processing
from .functions_pass import functions_processing
from .constants_pass import constants_processing
from .globals_pass import globals_processing
def processor(source_code, filename, module):
tree = ast.parse(source_code, filename)
print(ast.dump(tree, indent=4))
constants_processing(tree, module)
license_processing(tree, module)
globals_processing(tree, module)
functions_processing(tree, module)
def compile_to_ir(filename: str, output: str):
with open(filename) as f:
source = f.read()
module = ir.Module(name=filename)
module.data_layout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
module.triple = "bpf"
processor(source, filename, module)
wchar_size = module.add_metadata([ir.Constant(ir.IntType(32), 1),
"wchar_size",
ir.Constant(ir.IntType(32), 4)])
frame_pointer = module.add_metadata([ir.Constant(ir.IntType(32), 7),
"frame-pointer",
ir.Constant(ir.IntType(32), 2)])
module.add_named_metadata("llvm.module.flags", wchar_size)
module.add_named_metadata("llvm.module.flags", frame_pointer)
module.add_named_metadata("llvm.ident", ["llvmlite PythonBPF v0.0.0"])
with open(output, "w") as f:
f.write(str(module))
return output