def _(node, indLvl=0, *, ctx: Ctx = Ctx.default, recursive=False): if node.oper == '<=' and ctx == Ctx.module: dbg = dbgStr(node) assign = dump(node, indLvl, ctx=Ctx.useBlocking) if STYLE.useLogic: return dbg + ind(f"assign {assign}", indLvl) else: return dbg + ind(f"always @(*) {assign}", indLvl) else: oper = '=' if ctx == Ctx.useBlocking else node.oper res = DumpRes() subSelectorDump(node.lhs, res) if res.const: return ind( dump(node.lhs, ctx=Ctx.lhs) + " " + oper + " " + dump(node.expr, indLvl), indLvl) + ';' else: DEFS.add('_GET_MSK', 'w', '(1 << (w)) - 1') DEFS.add('_SET_FLD', 'x, sh, w, newval', '(newval << (sh)) | ((x) & ~(`_GET_MSK(w) << (sh)))') lsbStr = dump(chipo.simplify(res.base), ctx=Ctx.compact) wStr = dump(chipo.simplify(res.width), ctx=Ctx.compact) rhsStr = dump(node.expr) cmntStr = '' if (len(res.cmnt) == 1 and isinstance(node, chipo.BitExtract)) \ else f"/*{''.join(res.cmnt)}*/" rhs = f"`_SET_FLD({res.s}, {lsbStr}, {wStr}, {rhsStr}){cmntStr}" return ind( dump(node.lhs, ctx=Ctx.lhs) + " " + oper + " " + rhs, indLvl) + ';'
def subCmnt(node): if isinstance(node, chipo.BitExtract): lsbStr = dump(chipo.simplify(node._lsb()), ctx=Ctx.compact) msbStr = dump(chipo.simplify(node._msb()), ctx=Ctx.compact) if lsbStr != msbStr: return f"[{msbStr}:{lsbStr}]" return f"[{lsbStr}]" if isinstance(node, chipo.DotExpr): return "." + node._key() if isinstance(node, chipo.ItemExtract): idx = [dump(chipo.simplify(i), ctx=Ctx.compact) for i in node._idx()] idxStr = ",".join(idx) return f"[{idxStr}]" return ''
def dumpMsb(node): width = node.width if isinstance(width, chipo.Param): return f"{width.name}-1" if isinstance(width, chipo.Expr): return dump(chipo.simplify(width - 1), ctx=Ctx.compact) return f"{width-1}"
def simplifyBinExpr(be): lhs = simplify(be.args[0]) rhs = simplify(be.args[1]) if isInt(lhs) and isInt(rhs): return eval(f"{intVal(lhs)} {be.op} {intVal(rhs)}") if be.op == '+': if repr(lhs) == repr(rhs): # (+ E E) -> (* 2 E) return simplify(BinExpr('*', CInt(2), lhs)) for x, y in [(lhs, rhs), (rhs, lhs)]: if isIntVal(x, 0): # (+ E 0) --> E return simplify(y) e = simplifyPattern1(be.op, x, y) if e: return simplify(e) if be.op in {'+', '-'}: e = simplifyPattern2(be.op, lhs, rhs) if e: return simplify(e) e = simplifyPattern3(be.op, lhs, rhs) if e: return simplify(e) return BinExpr(be.op, lhs, rhs) # default don't do anything
def dumpSelector(node, ctx): res = DumpRes() subSelectorDump(node, res) msb = res.base + res.width - 1 lsbStr = dump(chipo.simplify(res.base), ctx=Ctx.compact) msbStr = dump(chipo.simplify(msb), ctx=Ctx.compact) cmntStr = '' if len(res.cmnt) == 1 and isinstance(node, chipo.BitExtract) \ else f"/*{''.join(res.cmnt)}*/" if res.const: if lsbStr != msbStr: return f"{res.s}{cmntStr}[{msbStr}:{lsbStr}]" return f"{res.s}{cmntStr}[{lsbStr}]" else: if ctx == Ctx.lhs: if res.concat: raise NotImplementedError( "lhs concat with variable indexing coming soon") return res.s else: wStr = dump(chipo.simplify(res.width), ctx=Ctx.compact) DEFS.add('_GET_MSK', 'w', '(1 << (w)) - 1') DEFS.add('_GET_FLD', 'x, sh, w', '((x) >> (sh)) & `_GET_MSK(w)') return f"`_GET_FLD({res.s}, {lsbStr}, {wStr}){cmntStr}"