Introduce middle-end

This commit is contained in:
Lixuanwang
2025-05-24 16:27:48 +08:00
parent ec8deeeebf
commit 338e5ef9a4
7 changed files with 97 additions and 1 deletions

View File

@@ -0,0 +1,22 @@
from llvmlite import ir
def parse_llvm_ir(ir_text: str) -> MiddleFunction:
"""将 LLVM IR 文本转换为 MiddleIR 结构(简化版)"""
module = ir.Module()
module.parse(ir_text)
# 提取第一个函数
func = list(module.functions)[0]
middle_func = MiddleFunction(func.name)
# 转换基本块和指令
for block in func.blocks:
mid_block = MiddleBasicBlock(block.name)
for instr in block.instructions:
opcode = instr.opcode
operands = [str(op) for op in instr.operands]
mid_instr = MiddleInstruction(opcode, operands)
mid_block.instructions.append(mid_instr)
middle_func.basic_blocks.append(mid_block)
return middle_func