Example #1
0
def __main__():
    argp = argparse.ArgumentParser(
        description="Parse and dump PseudoC program")
    argp.add_argument("file", help="Input file in PseudoC format")
    argp.add_argument("--repr",
                      action="store_true",
                      help="Dump __repr__ format of instructions")
    argp.add_argument("--roundtrip",
                      action="store_true",
                      help="Dump PseudoC asm")
    argp.add_argument("--addr-width",
                      type=int,
                      default=8,
                      help="Width of address field (%(default)d)")
    argp.add_argument("--inst-indent",
                      type=int,
                      default=4,
                      help="Indent of instructions (%(default)d)")
    args = argp.parse_args()

    p = Parser(args.file)
    cfg = p.parse()
    cfg.parser = p

    if args.roundtrip:
        p = AsmPrinter(cfg)
        p.addr_width = args.addr_width
        p.inst_indent = args.inst_indent
        p.print()
        sys.exit()

    print("Labels:", p.labels)
    if args.repr:
        core.SimpleExpr.simple_repr = False

    print("Basic blocks:")
    dump_bblocks(cfg, printer=repr if args.repr else str)

    with open(sys.argv[1] + ".0.dot", "w") as f:
        dot.dot(cfg, f)
Example #2
0
from asmprinter import AsmPrinter


argp = argparse.ArgumentParser(description="Parse and dump PseudoC program")
argp.add_argument("file", help="Input file in PseudoC format")
argp.add_argument("--repr", action="store_true", help="Dump __repr__ format of instructions")
argp.add_argument("--roundtrip", action="store_true", help="Dump PseudoC asm")
argp.add_argument("--addr-width", type=int, default=8, help="Width of address field (%(default)d)")
argp.add_argument("--inst-indent", type=int, default=4, help="Indent of instructions (%(default)d)")
args = argp.parse_args()

p = Parser(args.file)
cfg = p.parse()
cfg.parser = p

if args.roundtrip:
    p = AsmPrinter(cfg)
    p.addr_width = args.addr_width
    p.inst_indent = args.inst_indent
    p.print()
    sys.exit()

print("Labels:", p.labels)
if args.repr:
    core.SimpleExpr.simple_repr = False

print("Basic blocks:")
dump_bblocks(cfg, printer=repr if args.repr else str)

with open(sys.argv[1] + ".0.dot", "w") as f: dot.dot(cfg, f)
Example #3
0
def handle_file_unprotected(args):
    p = Parser(args.file)
    cfg = p.parse()
    cfg.parser = p

    # If we want to get asm back, i.e. stay close to the input, don't remove
    # trailing jumps. This will work OK for data flow algos, but will produce
    # broken or confusing output for control flow algos (for which asm output
    # shouldn't be used of course).
    # Update: it's unsafe to use this during dataflow analysis
    #if args.format != "asm":
    #    foreach_bblock(cfg, remove_trailing_jumps)

    if args.debug:
        with open(args.file + ".0.bb", "w") as f:
            dump_bblocks(cfg, f, no_graph_header=args.no_graph_header)
        with open(args.file + ".0.dot", "w") as f:
            dot.dot(cfg, f)

    if args.script:
        for s in args.script:
            mod = __import__(s)
            mod.apply(cfg)
    elif hasattr(p, "script"):
        for op_type, op_name in p.script:
            if op_type == "xform:":
                func = globals()[op_name]
                func(cfg)
            elif op_type == "xform_bblock:":
                func = globals()[op_name]
                foreach_bblock(cfg, func)
            elif op_type == "xform_inst:":
                func = globals()[op_name]
                foreach_inst(cfg, func)
            elif op_type == "script:":
                mod = __import__(op_name)
                mod.apply(cfg)
            else:
                assert 0

    if args.debug:
        with open(args.file + ".out.bb", "w") as f:
            dump_bblocks(cfg, f, no_graph_header=args.no_graph_header)
        with open(args.file + ".out.dot", "w") as f:
            dot.dot(cfg, f)

    if args.output and args.format != "none":
        out = open(args.output, "w")
    else:
        out = sys.stdout

    if args.no_comments:
        Inst.show_comments = False

    if args.format == "bblocks":
        p = CFGPrinter(cfg, out)
        if args.no_graph_header:
            p.print_graph_header = lambda: None
        p.inst_printer = repr if args.repr else str
        p.no_dead = args.no_dead
        p.print()
    elif args.format == "asm":
        p = AsmPrinter(cfg, out)
        p.no_dead = args.no_dead
        p.print()
    elif args.format == "c":
        #foreach_bblock(cfg, remove_trailing_jumps)
        cfg.number_postorder()
        Inst.trail = ";"
        cprinter.no_dead = args.no_dead
        cprinter.dump_c(cfg, out)

    if out is not sys.stdout:
        out.close()

    progdb.update_funcdb(cfg)

    return cfg
Example #4
0
def handle_file(args):
    p = Parser(args.file)
    cfg = p.parse()
    cfg.parser = p

    # If we want to get asm back, i.e. stay close to the input, don't remove
    # trailing jumps. This will work OK for data flow algos, but will produce
    # broken or confusing output for control flow algos (for which asm output
    # shouldn't be used of course).
    # Update: it's unsafe to use this during dataflow analysis
    #if args.format != "asm":
    #    foreach_bblock(cfg, remove_trailing_jumps)

    if args.debug:
        with open(args.file + ".0.bb", "w") as f:
            dump_bblocks(cfg, f)
        with open(args.file + ".0.dot", "w") as f:
            dot.dot(cfg, f)

    if args.script:
        mod = __import__(args.script)
        mod.apply(cfg)
    elif hasattr(p, "script"):
        for (type, xform) in p.script:
            func = globals()[xform]
            if type == "xform:":
                func(cfg)
            elif type == "xform_bblock:":
                foreach_bblock(cfg, func)
            else:
                assert 0

    if args.debug:
        with open(args.file + ".out.bb", "w") as f:
            dump_bblocks(cfg, f)
        with open(args.file + ".out.dot", "w") as f:
            dot.dot(cfg, f)

    if args.output:
        out = open(args.output, "w")
    else:
        out = sys.stdout

    if args.format == "bblocks":
        p = CFGPrinter(cfg, out)
        p.inst_printer = repr if args.repr else str
        p.no_dead = args.no_dead
        p.print()
    elif args.format == "asm":
        p = AsmPrinter(cfg, out)
        p.no_dead = args.no_dead
        p.print()
    elif args.format == "c":
        #foreach_bblock(cfg, remove_trailing_jumps)
        cfg.number_postorder()
        Inst.trail = ";"
        cprinter.no_dead = args.no_dead
        cprinter.dump_c(cfg, out)

    if args.output:
        out.close()

    return cfg