def ExtractBblTable(fun: ir.Fun, lst: List) -> Dict[int, ir.Bbl]: assert len(lst) % 2 == 0 it = iter(lst) out = {} for num_str in it: bbl_name = next(it) out[int(num_str)] = fun.GetBblOrAddForwardDeclaration(bbl_name) return out
def _GetOperand(unit: ir.Unit, fun: ir.Fun, ok: o.OP_KIND, v: Any) -> Any: if ok in o.OKS_LIST: assert isinstance(v, list) or v[0] == v[-1] == '"', f"operand {ok}: [{v}]" else: assert isinstance(v, str), f"bad operand {v} of type [{ok}]" if ok is o.OP_KIND.TYPE_LIST: out = [] for kind_name in v: kind = o.SHORT_STR_TO_RK.get(kind_name) assert kind is not None, f"bad kind name [{kind_name}]" out.append(kind) return out elif ok is o.OP_KIND.FUN: return unit.GetFunOrAddForwardDeclaration(v) elif ok is o.OP_KIND.BBL: return fun.GetBblOrAddForwardDeclaration(v) elif ok is o.OP_KIND.BBL_TAB: return ExtractBblTable(fun, v) elif ok is o.OP_KIND.MEM: return unit.GetMem(v) elif ok is o.OP_KIND.STK: return fun.GetStk(v) elif ok is o.OP_KIND.FUN_KIND: return o.SHORT_STR_TO_FK[v] elif ok is o.OP_KIND.DATA_KIND: rk = o.SHORT_STR_TO_RK.get(v) assert rk is not None, f"bad kind name [{v}]" return rk elif ok is o.OP_KIND.NAME: assert parse.RE_IDENTIFIER.match(v), f"bad identifier [{v}]" return v elif ok is o.OP_KIND.NAME_LIST: for x in v: assert parse.RE_IDENTIFIER.match(x), f"bad identifier [{x}]" return v elif ok is o.OP_KIND.MEM_KIND: return o.SHORT_STR_TO_MK[v] elif ok is o.OP_KIND.VALUE: return v elif ok is o.OP_KIND.BYTES: return ExtractBytes(v) elif ok is o.OP_KIND.JTB: return fun.GetJbl(v) else: raise ir.ParseError(f"cannot read op type: {ok}")