paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | import micropython |
paul@90 | 4 | from micropython.rsvp import raw |
paul@90 | 5 | import rsvp |
paul@0 | 6 | import sys |
paul@94 | 7 | import os |
paul@4 | 8 | |
paul@77 | 9 | code = None |
paul@77 | 10 | |
paul@62 | 11 | def show(importer, with_builtins=0, optimisations=None): |
paul@90 | 12 | make(importer, with_builtins, optimisations) |
paul@90 | 13 | show_code(code) |
paul@90 | 14 | |
paul@90 | 15 | def make(importer, with_builtins=0, optimisations=None): |
paul@62 | 16 | optimisations = optimisations or requested_optimisations |
paul@77 | 17 | global code |
paul@77 | 18 | code = importer.get_image(with_builtins, optimisations) |
paul@90 | 19 | |
paul@90 | 20 | def show_code(code): |
paul@77 | 21 | for i, x in enumerate(code): |
paul@22 | 22 | print i, x |
paul@22 | 23 | |
paul@135 | 24 | def show_table_usage(raw_table, slice_size=100): |
paul@135 | 25 | for x in xrange(0, len(raw_table), slice_size): |
paul@135 | 26 | table_slice = raw_table[x:x+slice_size] |
paul@135 | 27 | print "%6d" % (len(table_slice) - table_slice.count(None)), \ |
paul@135 | 28 | "".join(entry and "#" or "_" for entry in table_slice) |
paul@135 | 29 | |
paul@93 | 30 | def machine(importer): |
paul@137 | 31 | print "Making the image..." |
paul@137 | 32 | make(importer) |
paul@137 | 33 | rc = raw(importer.get_image()) |
paul@137 | 34 | print "Getting raw tables..." |
paul@137 | 35 | objtable = importer.get_object_table().as_raw() |
paul@137 | 36 | paramtable = importer.get_parameter_table().as_raw() |
paul@137 | 37 | print "Initialising the machine..." |
paul@137 | 38 | rm = rsvp.RSVPMachine(rc, objtable, paramtable) |
paul@93 | 39 | rm.pc = importer.code_location |
paul@90 | 40 | return rm |
paul@90 | 41 | |
paul@52 | 42 | def attrs(obj): |
paul@52 | 43 | for name, attr in obj.items(): |
paul@52 | 44 | print name, attr |
paul@52 | 45 | |
paul@62 | 46 | if __name__ == "__main__": |
paul@63 | 47 | args = sys.argv[2:] |
paul@94 | 48 | path = sys.path[:] |
paul@94 | 49 | |
paul@94 | 50 | if len(sys.argv) > 1: |
paul@94 | 51 | program = os.path.abspath(sys.argv[1]) |
paul@94 | 52 | path.append(os.path.split(program)[0]) |
paul@94 | 53 | else: |
paul@94 | 54 | program = None |
paul@94 | 55 | |
paul@94 | 56 | i = micropython.Importer(path, "-v" in args) |
paul@62 | 57 | |
paul@63 | 58 | if "-omax" in args: |
paul@63 | 59 | requested_optimisations = i.supported_optimisations |
paul@63 | 60 | else: |
paul@63 | 61 | requested_optimisations = [] |
paul@63 | 62 | for arg in args: |
paul@63 | 63 | if arg.startswith("-o"): |
paul@93 | 64 | for arg_part in arg[2:].split(","): |
paul@93 | 65 | requested_optimisations.append(arg_part) |
paul@63 | 66 | |
paul@62 | 67 | try: |
paul@62 | 68 | builtins = i.load_from_file("lib/builtins.py", "__builtins__") |
paul@94 | 69 | if program is None: |
paul@94 | 70 | print "Loading module micropython ..." |
paul@62 | 71 | m = i.load("micropython") |
paul@62 | 72 | else: |
paul@94 | 73 | print "Loading program", program, "..." |
paul@94 | 74 | m = i.load_from_file(program) |
paul@62 | 75 | except micropython.ProcessingError, exc: |
paul@62 | 76 | print repr(exc) |
paul@99 | 77 | if "-tb" in args: |
paul@99 | 78 | raise |
paul@37 | 79 | else: |
paul@62 | 80 | i.vacuum() |
paul@62 | 81 | ot = i.get_object_table() |
paul@62 | 82 | pt = i.get_parameter_table() |
paul@0 | 83 | |
paul@0 | 84 | # vim: tabstop=4 expandtab shiftwidth=4 |