class CodeVisitor(pc_util.Visitor):
def __init__(self, insn, root):
+ if not isinstance(root, pc_ast.Scope):
+ raise ValueError(root)
+
self.__root = root
self.__insn = insn
self.__decls = set()
super().__init__(root=root)
- self.__code[self.__header].emit(stmt="void")
- self.__code[self.__header].emit(stmt=f"oppc_{insn.name}(struct oppc_value const *insn) {{")
- with self.__code[self.__header]:
- for decl in self.__decls:
- self.__code[self.__header].emit(stmt=f"struct oppc_value {decl};")
+ for decl in self.__decls:
+ self.__code[self.__header].emit(stmt=f"struct oppc_value {decl};")
decls = sorted(filter(lambda decl: decl in insn.fields, self.__decls))
if decls:
self.__code[self.__header].emit()
symbol = pc_ast.Symbol(decl)
assign = pc_ast.AssignExpr(lvalue=symbol, rvalue=transient)
self.traverse(root=assign)
- with self[self.__header]:
- for (level, stmt) in self[assign]:
- self[self.__header].emit(stmt=stmt, level=level)
+ for (level, stmt) in self[assign]:
+ self[self.__header].emit(stmt=stmt, level=level)
for (lbit, rbit) in enumerate(insn.fields[decl]):
lsymbol = pc_ast.Symbol(decl)
rsymbol = Instruction()
rvalue = pc_ast.SubscriptExpr(index=rindex, subject=rsymbol)
assign = pc_ast.AssignExpr(lvalue=lvalue, rvalue=rvalue)
self.traverse(root=assign)
- with self[self.__header]:
- for (level, stmt) in self[assign]:
- self[self.__header].emit(stmt=stmt, level=level)
+ for (level, stmt) in self[assign]:
+ self[self.__header].emit(stmt=stmt, level=level)
self.__code[self.__header].emit()
if decls:
self.__code[self.__header].emit()
- self.__code[self.__footer].emit(stmt=f"}}")
- self.__code[self.__footer].emit()
def __iter__(self):
- yield from self.__code[self.__header]
- yield from self.__code[self.__root]
- yield from self.__code[self.__footer]
+ yield from self[self.__header]
+ for (level, stmt) in self[self.__root]:
+ yield ((level - 1), stmt)
+ yield from self[self.__footer]
def __getitem__(self, node):
return self.__code[node]