23 lines
750 B
Python
23 lines
750 B
Python
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
|