Get ex2 running

This commit is contained in:
2025-09-07 19:19:58 +05:30
parent 734a49b295
commit c0559639f2
11 changed files with 72 additions and 35 deletions

View File

@ -1,8 +1,8 @@
import ast
from llvmlite import ir
from .license_pass import license_processing
from .functions_pass import func_proc, functions_processing
from .constants_pass import constants_processing
from .functions_pass import func_proc
# from .constants_pass import constants_processing
from .globals_pass import globals_processing
@ -28,12 +28,10 @@ def processor(source_code, filename, module):
func_proc(tree, module, bpf_chunks)
# For now, we will parse the BPF specific parts of AST
# Big rewrite
# will worry later
# constants_processing(tree, module)
# license_processing(tree, module)
# globals_processing(tree, module)
license_processing(tree, module)
globals_processing(tree, module)
# functions_processing(tree, module)
@ -57,6 +55,7 @@ def compile_to_ir(filename: str, output: str):
module.add_named_metadata("llvm.ident", ["llvmlite PythonBPF v0.0.0"])
with open(output, "w") as f:
f.write(f"source_filename = \"{filename}\"\n")
f.write(str(module))
return output

View File

@ -13,9 +13,9 @@ def emit_function(module: ir.Module, name: str):
param.add_attribute("nocapture")
func.attributes.add("nounwind")
# func.attributes.add("\"frame-pointer\"=\"all\"")
# func.attributes.add("no-trapping-math", "true")
# func.attributes.add("stack-protector-buffer-size", "8")
# func.attributes.add("\"frame-pointer\"=\"all\"")
# func.attributes.add("no-trapping-math", "true")
# func.attributes.add("stack-protector-buffer-size", "8")
block = func.append_basic_block(name="entry")
builder = ir.IRBuilder(block)
@ -70,17 +70,17 @@ def process_func_body(module, builder, func_node, func):
# Handle print statement
for arg in call.args:
if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
fmt_str = arg.value + "\n"
fmt_str = arg.value + "\n" + "\0"
# Create a global variable for the format string
fmt_gvar = ir.GlobalVariable(
module, ir.ArrayType(ir.IntType(8), len(fmt_str)), name=f"{func.name}____fmt")
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)),
bytearray(fmt_str.encode("utf8"))
)
fmt_gvar.linkage = "internal"
fmt_gvar.align = 1
fmt_gvar.align = 1 # type: ignore
# Cast the global variable to i8*
fmt_ptr = builder.bitcast(
@ -129,6 +129,8 @@ def process_bpf_chunk(func_node, module):
func.linkage = "dso_local"
func.attributes.add("nounwind")
func.attributes.add("noinline")
func.attributes.add("optnone")
if func_node.args.args:
# Only look at the first argument for now

View File

@ -31,8 +31,8 @@ def globals_processing(tree, module: ir.Module):
collected = ["LICENSE"]
for node in tree.body:
if isinstance(node, ast.FunctionDef) and len(node.decorator_list) == 1:
dec = node.decorator_list[0]
if isinstance(node, ast.FunctionDef) and len(node.decorator_list) == 2:
dec = node.decorator_list[1]
if (
isinstance(dec, ast.Call)
and isinstance(dec.func, ast.Name)