github/workflows/pycopy-test: Upgrade Pycopy to 3.6.1.
[ScratchABlock.git] / parse_asm.py
blobeb4029c726d2ebea65df5d3ffc8cd41c3f0961c3
1 #!/usr/bin/env python3
2 import sys
3 import argparse
4 from parser import *
5 import core
6 import dot
7 from xform import foreach_inst
8 from asmprinter import AsmPrinter
11 def __main__():
12 argp = argparse.ArgumentParser(description="Parse and dump PseudoC program")
13 argp.add_argument("file", help="Input file in PseudoC format")
14 argp.add_argument("--repr", action="store_true", help="Dump __repr__ format of instructions")
15 argp.add_argument("--roundtrip", action="store_true", help="Dump PseudoC asm")
16 argp.add_argument("--addr-width", type=int, default=8, help="Width of address field (%(default)d)")
17 argp.add_argument("--inst-indent", type=int, default=4, help="Indent of instructions (%(default)d)")
18 args = argp.parse_args()
20 p = Parser(args.file)
21 cfg = p.parse()
22 cfg.parser = p
24 if args.roundtrip:
25 p = AsmPrinter(cfg)
26 p.addr_width = args.addr_width
27 p.inst_indent = args.inst_indent
28 p.print()
29 sys.exit()
31 print("Labels:", p.labels)
32 if args.repr:
33 core.SimpleExpr.simple_repr = False
35 print("Basic blocks:")
36 dump_bblocks(cfg, printer=repr if args.repr else str)
38 with open(sys.argv[1] + ".0.dot", "w") as f: dot.dot(cfg, f)
41 if __name__ == "__main__":
42 __main__()