[lab3] add print support for functions, blocks and instructions
This commit is contained in:
@@ -2,14 +2,26 @@ class MiddleFunction:
|
|||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.basic_blocks = [] # List[MiddleBasicBlock]
|
self.basic_blocks = [] # List[MiddleBasicBlock]
|
||||||
|
def __repr__(self):
|
||||||
|
return f"Function({self.name}):\n" + "\n".join(str(block) for block in self.basic_blocks)
|
||||||
|
def __str__(self):
|
||||||
|
return self.__repr__()
|
||||||
|
|
||||||
class MiddleBasicBlock:
|
class MiddleBasicBlock:
|
||||||
def __init__(self, name: str):
|
def __init__(self, name: str):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.instructions = [] # List[MiddleInstruction]
|
self.instructions = [] # List[MiddleInstruction]
|
||||||
|
def __repr__(self):
|
||||||
|
return f"BasicBlock({self.name}):\n" + "\n".join(str(instr) for instr in self.instructions)
|
||||||
|
def __str__(self):
|
||||||
|
return self.__repr__()
|
||||||
|
|
||||||
class MiddleInstruction:
|
class MiddleInstruction:
|
||||||
def __init__(self, opcode: str, operands: list, dest=None):
|
def __init__(self, opcode: str, operands: list, dest=None):
|
||||||
self.opcode = opcode # e.g., 'add'
|
self.opcode = opcode # e.g., 'add'
|
||||||
self.operands = operands # e.g., ['5', '7']
|
self.operands = operands # e.g., ['5', '7']
|
||||||
self.dest = dest # e.g., '%0'
|
self.dest = dest # e.g., '%0'
|
||||||
|
def __repr__(self):
|
||||||
|
return f"{self.dest} = {self.opcode} " + ", ".join(self.operands) if self.dest else f"{self.opcode} " + ", ".join(self.operands)
|
||||||
|
def __str__(self):
|
||||||
|
return self.__repr__()
|
||||||
@@ -18,8 +18,7 @@ def parse_llvm_ir(ir_text: str) -> MiddleFunction:
|
|||||||
for instr in block.instructions:
|
for instr in block.instructions:
|
||||||
opcode = instr.opcode
|
opcode = instr.opcode
|
||||||
operands = [str(op) for op in instr.operands]
|
operands = [str(op) for op in instr.operands]
|
||||||
# 调试输出
|
|
||||||
print(f"Instruction: {instr}, Opcode: {opcode}, Operands: {operands}")
|
|
||||||
# 如果是有返回值的指令(如 add),第一个操作数是 dest
|
# 如果是有返回值的指令(如 add),第一个操作数是 dest
|
||||||
dest = None
|
dest = None
|
||||||
if hasattr(instr, 'name') and instr.name:
|
if hasattr(instr, 'name') and instr.name:
|
||||||
|
|||||||
@@ -3,8 +3,9 @@ from sysy.utils.ir_parser import parse_llvm_ir
|
|||||||
from sysy.backend.x86_emitter import X86Emitter
|
from sysy.backend.x86_emitter import X86Emitter
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) != 3:
|
if len(sys.argv) != 2:
|
||||||
print("Usage: sysyc.py <input.ll> <output.s>")
|
# print("Usage: sysyc.py <input.ll> <output.s>")
|
||||||
|
print("Usage: sysyc.py <input.ll>")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# 读取 LLVM IR
|
# 读取 LLVM IR
|
||||||
@@ -13,11 +14,12 @@ def main():
|
|||||||
|
|
||||||
# 解析并生成汇编
|
# 解析并生成汇编
|
||||||
func = parse_llvm_ir(ir_text)
|
func = parse_llvm_ir(ir_text)
|
||||||
|
print(func)
|
||||||
asm = X86Emitter().emit_function(func)
|
asm = X86Emitter().emit_function(func)
|
||||||
|
|
||||||
# 写入文件
|
# 写入文件
|
||||||
with open(sys.argv[2], 'w') as f:
|
# with open(sys.argv[2], 'w') as f:
|
||||||
f.write(asm)
|
# f.write(asm)
|
||||||
|
print(asm)
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user