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