paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@233 | 3 | import micropython.cmd |
paul@353 | 4 | import micropython.report |
paul@149 | 5 | from micropython.graph import get_graph |
paul@90 | 6 | import rsvp |
paul@0 | 7 | import sys |
paul@94 | 8 | import os |
paul@4 | 9 | |
paul@350 | 10 | libdirs = [ |
paul@350 | 11 | os.path.join(os.path.split(__file__)[0], "lib"), |
paul@350 | 12 | "/usr/share/micropython/lib" |
paul@350 | 13 | ] |
paul@350 | 14 | |
paul@90 | 15 | def show_code(code): |
paul@77 | 16 | for i, x in enumerate(code): |
paul@22 | 17 | print i, x |
paul@22 | 18 | |
paul@135 | 19 | def show_table_usage(raw_table, slice_size=100): |
paul@135 | 20 | for x in xrange(0, len(raw_table), slice_size): |
paul@135 | 21 | table_slice = raw_table[x:x+slice_size] |
paul@135 | 22 | print "%6d" % (len(table_slice) - table_slice.count(None)), \ |
paul@135 | 23 | "".join(entry and "#" or "_" for entry in table_slice) |
paul@135 | 24 | |
paul@52 | 25 | def attrs(obj): |
paul@52 | 26 | for name, attr in obj.items(): |
paul@52 | 27 | print name, attr |
paul@52 | 28 | |
paul@228 | 29 | # Main program. |
paul@228 | 30 | |
paul@228 | 31 | if __name__ == "__main__": |
paul@228 | 32 | args = sys.argv[2:] |
paul@350 | 33 | path = libdirs + sys.path[:] |
paul@228 | 34 | |
paul@268 | 35 | if "--help" in sys.argv: |
paul@268 | 36 | print "Optimisations:" |
paul@268 | 37 | print micropython.cmd.show_optimisations() |
paul@268 | 38 | sys.exit(1) |
paul@268 | 39 | |
paul@315 | 40 | if len(sys.argv) > 1 and sys.argv[1] != "-": |
paul@228 | 41 | filename = os.path.abspath(sys.argv[1]) |
paul@228 | 42 | path.append(os.path.split(filename)[0]) |
paul@228 | 43 | else: |
paul@228 | 44 | filename = None |
paul@228 | 45 | |
paul@280 | 46 | # Load the program. |
paul@167 | 47 | |
paul@62 | 48 | try: |
paul@314 | 49 | p = micropython.cmd.get_program(path, args) |
paul@314 | 50 | i = p.get_importer() |
paul@314 | 51 | |
paul@326 | 52 | if filename is None or filename == "-": |
paul@94 | 53 | print "Loading module micropython ..." |
paul@314 | 54 | m = i.load("micropython") |
paul@62 | 55 | else: |
paul@314 | 56 | print "Loading from", filename, "..." |
paul@314 | 57 | m = i.load_from_file(filename) |
paul@228 | 58 | |
paul@353 | 59 | p.finalise() |
paul@433 | 60 | b = i.get_module("__builtins__") |
paul@353 | 61 | |
paul@353 | 62 | # Make a report. |
paul@353 | 63 | |
paul@353 | 64 | if "-d" in args: |
paul@353 | 65 | try: |
paul@353 | 66 | directory = args[args.index("-d") + 1] |
paul@353 | 67 | except IndexError: |
paul@353 | 68 | print "No directory specified. Not generating report." |
paul@353 | 69 | else: |
paul@353 | 70 | print "Generating report in", directory |
paul@357 | 71 | micropython.report.report(p, directory) |
paul@353 | 72 | |
paul@424 | 73 | if "-G" in args: |
paul@424 | 74 | try: |
paul@424 | 75 | graph = args[args.index("-G") + 1] |
paul@424 | 76 | except IndexError: |
paul@424 | 77 | print "No file specified. Not generating graph." |
paul@424 | 78 | else: |
paul@424 | 79 | print "Generating graph as", graph |
paul@424 | 80 | f = open(graph, "w") |
paul@424 | 81 | try: |
paul@435 | 82 | get_graph(p, f, filename) |
paul@424 | 83 | finally: |
paul@424 | 84 | f.close() |
paul@424 | 85 | |
paul@280 | 86 | # Build the program. |
paul@228 | 87 | |
paul@228 | 88 | if "-m" in args or "-t" in args: |
paul@246 | 89 | rm = rsvp.machine(p, debug=("-g" in args), abort_upon_exception=("-x" in args)) |
paul@293 | 90 | |
paul@228 | 91 | if "-t" in args: |
paul@228 | 92 | success = rm.test(m) |
paul@228 | 93 | print "Test successful?", success |
paul@187 | 94 | |
paul@293 | 95 | print "RSVP machine: rm = %r" % rm |
paul@293 | 96 | |
paul@286 | 97 | ot = p.get_object_table() |
paul@286 | 98 | pt = p.get_parameter_table() |
paul@286 | 99 | |
paul@293 | 100 | print "Object table: ot = %r" % ot |
paul@293 | 101 | print "Parameter table: pt = %r" % pt |
paul@293 | 102 | print "Importer: i = %r" % i |
paul@293 | 103 | print "Program: p = %r" % p |
paul@293 | 104 | print "Module: m = %r" % m |
paul@433 | 105 | print "Built-ins: b = %r" % b |
paul@293 | 106 | |
paul@280 | 107 | # Report any errors. |
paul@280 | 108 | |
paul@280 | 109 | except micropython.ProcessingError, exc: |
paul@280 | 110 | print repr(exc) |
paul@280 | 111 | if "-tb" in args: |
paul@280 | 112 | raise |
paul@407 | 113 | elif "-exit" in args: |
paul@407 | 114 | sys.exit(1) |
paul@280 | 115 | |
paul@441 | 116 | except KeyboardInterrupt: |
paul@441 | 117 | if "-exit" in args: |
paul@441 | 118 | sys.exit(2) |
paul@441 | 119 | else: |
paul@441 | 120 | raise |
paul@441 | 121 | |
paul@0 | 122 | # vim: tabstop=4 expandtab shiftwidth=4 |