add basic IR gen strategy

This commit is contained in:
2025-10-15 21:25:53 +05:30
parent eb4ee64ee5
commit 8372111616
3 changed files with 68 additions and 11 deletions

View File

@ -0,0 +1,15 @@
from pythonbpf.debuginfo import DebugInfoGenerator
def debug_info_generation(struct, llvm_module):
generator = DebugInfoGenerator(llvm_module)
# this is sample debug info generation
# i64type = generator.get_uint64_type()
struct_type = generator.create_struct_type([], 64 * 4, is_distinct=True)
global_var = generator.create_global_var_debug_info(
struct.name, struct_type, is_local=False
)
return global_var

View File

@ -1,12 +1,16 @@
import logging
from pythonbpf.vmlinux_parser.dependency_handler import DependencyHandler
from ..dependency_handler import DependencyHandler
from .debug_info_gen import debug_info_generation
from ..dependency_node import DependencyNode
import llvmlite.ir as ir
logger = logging.getLogger(__name__)
class IRGenerator:
def __init__(self, module, handler: DependencyHandler):
self.module = module
# get the assignments dict and add this stuff to it.
def __init__(self, llvm_module, handler: DependencyHandler, assignment=None):
self.llvm_module = llvm_module
self.handler: DependencyHandler = handler
self.generated: list[str] = []
if not handler.is_ready:
@ -15,22 +19,48 @@ class IRGenerator:
)
for struct in handler:
self.struct_processor(struct)
print()
def struct_processor(self, struct):
if struct.name not in self.generated:
print(f"IR generating for {struct.name}")
print(f"Struct is {struct}")
for dependency in struct.depends_on:
if dependency not in self.generated:
dep_node_from_dependency = self.handler[dependency]
self.struct_processor(dep_node_from_dependency)
self.generated.append(dependency)
# write actual processor logic here after assuming all dependencies are resolved
# actual processor logic here after assuming all dependencies are resolved
# this part cannot yet resolve circular dependencies. Gets stuck on an infinite loop during that.
self.gen_ir(struct)
self.generated.append(struct.name)
def struct_name_generator(
self,
) -> None:
pass
def gen_ir(self, struct):
# currently we generate all possible field accesses for CO-RE and put into the assignment table
debug_info = debug_info_generation(struct, self.llvm_module)
field_index = 0
for field_name, field in struct.fields.items():
# does not take arrays and similar types into consideration yet.
field_co_re_name = self._struct_name_generator(struct, field, field_index)
field_index += 1
globvar = ir.GlobalVariable(
self.llvm_module, ir.IntType(64), name=field_co_re_name
)
globvar.linkage = "external"
globvar.set_metadata("llvm.preserve.access.index", debug_info)
print()
def _struct_name_generator(
self, struct: DependencyNode, field, field_index: int
) -> str:
if struct.name.startswith("struct_"):
name = (
"llvm."
+ struct.name.removeprefix("struct_")
+ f":0:{field.offset}"
+ "$"
+ f"0:{field_index}"
)
return name
else:
raise TypeError(
"Name generation cannot occur due to type name not starting with struct"
)