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