这段 IR 有几条
(每道题开头都有同一段:三地址码用元组表示——(op, dst, a, b),如 ("+","t2","a","t1") 是 t2 = a + t1、("const","t1",2,None) 是 t1 = 2。一个程序是这样一串元组。count_ops 数几条、temps_of 交回定义的临时名。)
一段三地址码,数它有几条指令:
def count_ops(prog):
"""三地址码程序一共几条指令。"""
return len(prog)
def temps_of(prog):
"""按出现顺序交回程序里定义的临时变量名(每条 IR 的 dst)。"""
return [t[1] for t in prog]
prog = [("const", "t1", 2, None), ("+", "t2", "a", "t1"), ("*", "t3", "t2", "b")]
print(count_ops(prog))
全部评论