From 3ca6de624df4981c36cd4b697004cdac9427830c Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Sun, 14 Jan 2024 23:00:49 +0300 Subject: [PATCH] oppc/code: support basic fields initialization --- src/openpower/oppc/pc_code.py | 40 +++++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/src/openpower/oppc/pc_code.py b/src/openpower/oppc/pc_code.py index d4086483..47aded9d 100644 --- a/src/openpower/oppc/pc_code.py +++ b/src/openpower/oppc/pc_code.py @@ -13,6 +13,9 @@ class Transient(pc_ast.Node): return super().__init__() + def __repr__(self): + return f"{hex(id(self))}@{self.__class__.__name__}({self.__value}, {self.__bits})" + def __str__(self): return f"oppc_transient(&(struct oppc_value){{}}, {self.__value}, {self.__bits})" @@ -23,15 +26,19 @@ class Call(pc_ast.Dataclass): stmt: bool +class Instruction(pc_ast.Node): + pass + + class CodeVisitor(pc_util.Visitor): def __init__(self, insn, root): self.__root = root self.__insn = insn self.__attrs = {} + self.__decls = set() self.__header = object() self.__footer = object() self.__code = collections.defaultdict(lambda: pc_util.Code()) - self.__decls = collections.defaultdict(list) self.__regfetch = collections.defaultdict(list) self.__regstore = collections.defaultdict(list) self.__pseudocode = pc_pseudocode.PseudocodeVisitor(root=root) @@ -43,6 +50,30 @@ class CodeVisitor(pc_util.Visitor): with self.__code[self.__header]: for decl in self.__decls: self.__code[self.__header].emit(stmt=f"struct oppc_value {decl};") + self.__code[self.__header].emit() + for decl in filter(lambda decl: decl in insn.fields, sorted(self.__decls)): + bits = f"{len(insn.fields[decl])}" + transient = Transient(bits=bits) + 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 (lbit, rbit) in enumerate(insn.fields[decl]): + lsymbol = pc_ast.Symbol(decl) + rsymbol = Instruction() + lindex = Transient(value=str(lbit)) + rindex = Transient(value=str(rbit)) + lvalue = pc_ast.SubscriptExpr(index=lindex, subject=lsymbol) + 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) + self.__code[self.__header].emit() + self.__code[self.__header].emit() self.__code[self.__footer].emit(stmt=f"}}") def __iter__(self): @@ -587,9 +618,14 @@ class CodeVisitor(pc_util.Visitor): yield node with self.pseudocode(node=node): if str(node) not in ("fallthrough",): - self.__decls[str(node)].append(node) + self.__decls.add(str(node)) self[node].emit(stmt=f"&{str(node)}") + @pc_util.Hook(Instruction) + def Instruction(self, node): + yield node + self[node].emit("insn") + @pc_util.Hook(pc_ast.Node) def Node(self, node): raise NotImplementedError(type(node)) -- 2.30.2