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@479 | 29 | def reset(): |
paul@479 | 30 | global rm |
paul@479 | 31 | rm = rsvp.machine(p) |
paul@479 | 32 | |
paul@228 | 33 | # Main program. |
paul@228 | 34 | |
paul@228 | 35 | if __name__ == "__main__": |
paul@228 | 36 | args = sys.argv[2:] |
paul@350 | 37 | path = libdirs + sys.path[:] |
paul@228 | 38 | |
paul@268 | 39 | if "--help" in sys.argv: |
paul@268 | 40 | print "Optimisations:" |
paul@268 | 41 | print micropython.cmd.show_optimisations() |
paul@268 | 42 | sys.exit(1) |
paul@268 | 43 | |
paul@315 | 44 | if len(sys.argv) > 1 and sys.argv[1] != "-": |
paul@228 | 45 | filename = os.path.abspath(sys.argv[1]) |
paul@228 | 46 | path.append(os.path.split(filename)[0]) |
paul@228 | 47 | else: |
paul@228 | 48 | filename = None |
paul@228 | 49 | |
paul@280 | 50 | # Load the program. |
paul@167 | 51 | |
paul@62 | 52 | try: |
paul@314 | 53 | p = micropython.cmd.get_program(path, args) |
paul@314 | 54 | i = p.get_importer() |
paul@314 | 55 | |
paul@326 | 56 | if filename is None or filename == "-": |
paul@94 | 57 | print "Loading module micropython ..." |
paul@314 | 58 | m = i.load("micropython") |
paul@62 | 59 | else: |
paul@314 | 60 | print "Loading from", filename, "..." |
paul@314 | 61 | m = i.load_from_file(filename) |
paul@228 | 62 | |
paul@353 | 63 | p.finalise() |
paul@433 | 64 | b = i.get_module("__builtins__") |
paul@353 | 65 | |
paul@353 | 66 | # Make a report. |
paul@353 | 67 | |
paul@353 | 68 | if "-d" in args: |
paul@353 | 69 | try: |
paul@353 | 70 | directory = args[args.index("-d") + 1] |
paul@353 | 71 | except IndexError: |
paul@353 | 72 | print "No directory specified. Not generating report." |
paul@353 | 73 | else: |
paul@353 | 74 | print "Generating report in", directory |
paul@357 | 75 | micropython.report.report(p, directory) |
paul@353 | 76 | |
paul@424 | 77 | if "-G" in args: |
paul@424 | 78 | try: |
paul@424 | 79 | graph = args[args.index("-G") + 1] |
paul@424 | 80 | except IndexError: |
paul@424 | 81 | print "No file specified. Not generating graph." |
paul@424 | 82 | else: |
paul@424 | 83 | print "Generating graph as", graph |
paul@424 | 84 | f = open(graph, "w") |
paul@424 | 85 | try: |
paul@435 | 86 | get_graph(p, f, filename) |
paul@424 | 87 | finally: |
paul@424 | 88 | f.close() |
paul@424 | 89 | |
paul@280 | 90 | # Build the program. |
paul@228 | 91 | |
paul@228 | 92 | if "-m" in args or "-t" in args: |
paul@246 | 93 | rm = rsvp.machine(p, debug=("-g" in args), abort_upon_exception=("-x" in args)) |
paul@293 | 94 | |
paul@228 | 95 | if "-t" in args: |
paul@228 | 96 | success = rm.test(m) |
paul@228 | 97 | print "Test successful?", success |
paul@187 | 98 | |
paul@293 | 99 | print "RSVP machine: rm = %r" % rm |
paul@293 | 100 | |
paul@286 | 101 | ot = p.get_object_table() |
paul@286 | 102 | pt = p.get_parameter_table() |
paul@286 | 103 | |
paul@293 | 104 | print "Object table: ot = %r" % ot |
paul@293 | 105 | print "Parameter table: pt = %r" % pt |
paul@293 | 106 | print "Importer: i = %r" % i |
paul@293 | 107 | print "Program: p = %r" % p |
paul@293 | 108 | print "Module: m = %r" % m |
paul@433 | 109 | print "Built-ins: b = %r" % b |
paul@293 | 110 | |
paul@280 | 111 | # Report any errors. |
paul@280 | 112 | |
paul@280 | 113 | except micropython.ProcessingError, exc: |
paul@280 | 114 | print repr(exc) |
paul@280 | 115 | if "-tb" in args: |
paul@280 | 116 | raise |
paul@407 | 117 | elif "-exit" in args: |
paul@407 | 118 | sys.exit(1) |
paul@280 | 119 | |
paul@441 | 120 | except KeyboardInterrupt: |
paul@441 | 121 | if "-exit" in args: |
paul@441 | 122 | sys.exit(2) |
paul@441 | 123 | else: |
paul@441 | 124 | raise |
paul@441 | 125 | |
paul@454 | 126 | else: |
paul@454 | 127 | if "-exit" in args and "-t" in args: |
paul@454 | 128 | if success: |
paul@454 | 129 | sys.exit(0) |
paul@454 | 130 | else: |
paul@454 | 131 | sys.exit(1) |
paul@454 | 132 | |
paul@0 | 133 | # vim: tabstop=4 expandtab shiftwidth=4 |