def get_runtime_typechecks(xform): # type: (XForm) -> List[TypeConstraint] """ Given a XForm build a list of runtime type checks neccessary to determine if it applies. We have 2 types of runtime checks: 1) typevar tv belongs to typeset T - needed for free tvs whose typeset is constrainted by their use in the dst pattern 2) tv1 == tv2 where tv1 and tv2 are derived TVs - caused by unification of non-bijective functions """ check_l = [] # type: List[TypeConstraint] # 1) Perform ti only on the source RTL. Accumulate any free tvs that have a # different inferred type in src, compared to the type inferred for both # src and dst. symtab = {} # type: VarAtomMap src_copy = xform.src.copy(symtab) src_typenv = get_type_env(ti_rtl(src_copy, TypeEnv())) for v in xform.ti.vars: if not v.has_free_typevar(): continue # In rust the local variable containing a free TV associated with var v # has name typeof_v. We rely on the python TVs having the same name. assert "typeof_{}".format(v) == xform.ti[v].name if v not in symtab: # We can have singleton vars defined only on dst. Ignore them assert v.get_typevar().singleton_type() is not None continue inner_v = symtab[v] assert isinstance(inner_v, Var) src_ts = src_typenv[inner_v].get_typeset() xform_ts = xform.ti[v].get_typeset() assert xform_ts.issubset(src_ts) if src_ts != xform_ts: check_l.append(InTypeset(xform.ti[v], xform_ts)) # 2,3) Add any constraints that appear in xform.ti check_l.extend(xform.ti.constraints) return check_l
def verify_semantics(inst, src, xforms): # type: (Instruction, Rtl, InstructionSemantics) -> None """ Verify that the semantics transforms in xforms correctly describe the instruction described by the src Rtl. This involves checking that: 0) src is a single instance of inst 1) For all x\in xforms x.src is a single instance of inst 2) For any concrete values V of Literals in inst: For all concrete typing T of inst: Exists single x \in xforms that applies to src conretazied to V and T """ # 0) The source rtl is always a single instance of inst assert len(src.rtl) == 1 and src.rtl[0].expr.inst == inst # 1) For all XForms x, x.src is a single instance of inst for x in xforms: assert len(x.src.rtl) == 1 and x.src.rtl[0].expr.inst == inst variants = [src] # type: List[Rtl] # 2) For all enumerated immediates, compute all the possible # versions of src with the concrete value filled in. for i in inst.imm_opnums: op = inst.ins[i] if not (isinstance(op.kind, ImmediateKind) and op.kind.is_enumerable()): continue new_variants = [] # type: List[Rtl] for rtl_var in variants: s = {v: v for v in rtl_var.vars()} # type: VarAtomMap arg = rtl_var.rtl[0].expr.args[i] assert isinstance(arg, Var) for val in op.kind.possible_values(): s[arg] = val new_variants.append(rtl_var.copy(s)) variants = new_variants # For any possible version of the src with concrete enumerated immediates for src in variants: # 2) Any possible typing should be covered by exactly ONE semantic # XForm src = src.copy({}) typenv = get_type_env(ti_rtl(src, TypeEnv())) typenv.normalize() typenv = typenv.extract() for t in typenv.concrete_typings(): matching_xforms = [] # type: List[XForm] for x in xforms: if src.substitution(x.src, {}) is None: continue # Translate t using x.symtab t = {x.symtab[str(v)]: tv for (v, tv) in t.items()} if (x.ti.permits(t)): matching_xforms.append(x) assert len(matching_xforms) == 1,\ ("Possible typing {} of {} not matched by exactly one case " + ": {}").format(t, src.rtl[0], matching_xforms)