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