def add_path(line): if m := match(line, ("goto", Any, ":svs")): path2 = path for _, v_idx, v_val in m.svs: path2 = replace(path2, ("var", v_idx), v_val) return path2 + [line]
def make(trace): res = [] for idx, line in enumerate(trace): if m := match(line, ("if", ":cond", ":if_true", ":if_false")): res.append(("if", m.cond, make(m.if_true), make(m.if_false))) elif m := match(line, ("label", ":jd", ":vars", ...)): jd, vars = m.jd, m.vars try: before, inside, remaining, cond = to_while(trace[idx + 1:], jd) except Exception: continue inside = inside # + [str(inside)] inside = make(inside) remaining = make(remaining) for _, v_idx, v_val in vars: before = replace(before, ("var", v_idx), v_val) before = make(before) res.extend(before) res.append(("while", cond, inside, repr(jd), vars)) res.extend(remaining) return res
def replace_max_with_MAX(exp): if opcode(exp) != 'max': return exp, None exp = max_to_add(exp) res = exp for e in exp: if opcode(e) == 'max': res = e exp = replace(exp, res, 'MAX') exp = simplify(exp) return exp, res
def replace_max_with_MAX(exp): if opcode(exp) != "max": return exp, None exp = max_to_add(exp) res = exp for e in exp: if opcode(e) == "max": res = e exp = replace(exp, res, "MAX") exp = simplify(exp) return exp, res
elif ((m := match(stor_id, ("stor", Any, Any, ("map", Any, ":loc")))) or (m := match(stor_id, ("stor", Any, Any, ("array", Any, ":loc")))) or (m := match(stor_id, ("struct", ":loc")))): # for arrays, we don't want 'address' at the end of the name. looks better new_name = name.split("Address")[0] if len(new_name) > 0: name = new_name loc_id = get_loc(m.loc) used_locs.add(("array", loc_id)) for src, pattern in storages_assoc.items(): if get_loc(pattern) == loc_id: pattern = replace(pattern, ("loc", loc_id), ("name", name, loc_id)) storages_assoc[src] = pattern else: logger.warning("storage pattern not found") """ find storage names based on function name """ def find_storage_names(functions): res = {}
res.append(('if', cond, make(if_true), make(if_false))) elif line ~ ('label', :jd, :vars, ...): try: before, inside, remaining, cond = to_while(trace[idx+1:], jd) except: continue return trace inside = inside #+ [str(inside)] inside = make(inside) remaining = make(remaining) for _, v_idx, v_val in vars: before = replace(before, ('var', v_idx), v_val) before = make(before) res.extend(before) res.append(('while', cond, inside, repr(jd), vars)) res.extend(remaining) return res elif line ~ ('goto', :jd, :setvars): res.append(('continue', repr(jd), setvars)) else: res.append(line)
elif stor_id ~ ('stor', _, _, ('map', _, :loc)) or \ stor_id ~ ('stor', _, _, ('array', _, :loc)) or \ stor_id ~ ('struct', :loc): # for arrays, we don't want 'address' at the end of the name. looks better new_name = name.split('Address')[0] if len(new_name) > 0: name = new_name loc_id = get_loc(loc) used_locs.add(('array', loc_id)) for src, pattern in storages_assoc.items(): if get_loc(pattern) == loc_id: pattern = replace(pattern, ('loc', loc_id), ('name', name, loc_id)) storages_assoc[src] = pattern else: logger.warning('storage pattern not found') ''' find storage names based on function name ''' def find_storage_names(functions): res = {} for func in functions: