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@93 | 24 | def machine(importer): |
paul@93 | 25 | rc = raw(importer.code) |
paul@90 | 26 | rm = rsvp.RSVPMachine(rc) |
paul@93 | 27 | rm.pc = importer.code_location |
paul@90 | 28 | return rm |
paul@90 | 29 | |
paul@52 | 30 | def attrs(obj): |
paul@52 | 31 | for name, attr in obj.items(): |
paul@52 | 32 | print name, attr |
paul@52 | 33 | |
paul@62 | 34 | if __name__ == "__main__": |
paul@63 | 35 | args = sys.argv[2:] |
paul@94 | 36 | path = sys.path[:] |
paul@94 | 37 | |
paul@94 | 38 | if len(sys.argv) > 1: |
paul@94 | 39 | program = os.path.abspath(sys.argv[1]) |
paul@94 | 40 | path.append(os.path.split(program)[0]) |
paul@94 | 41 | else: |
paul@94 | 42 | program = None |
paul@94 | 43 | |
paul@94 | 44 | i = micropython.Importer(path, "-v" in args) |
paul@62 | 45 | |
paul@63 | 46 | if "-omax" in args: |
paul@63 | 47 | requested_optimisations = i.supported_optimisations |
paul@63 | 48 | else: |
paul@63 | 49 | requested_optimisations = [] |
paul@63 | 50 | for arg in args: |
paul@63 | 51 | if arg.startswith("-o"): |
paul@93 | 52 | for arg_part in arg[2:].split(","): |
paul@93 | 53 | requested_optimisations.append(arg_part) |
paul@63 | 54 | |
paul@62 | 55 | try: |
paul@62 | 56 | builtins = i.load_from_file("lib/builtins.py", "__builtins__") |
paul@94 | 57 | if program is None: |
paul@94 | 58 | print "Loading module micropython ..." |
paul@62 | 59 | m = i.load("micropython") |
paul@62 | 60 | else: |
paul@94 | 61 | print "Loading program", program, "..." |
paul@94 | 62 | m = i.load_from_file(program) |
paul@62 | 63 | except micropython.ProcessingError, exc: |
paul@62 | 64 | print repr(exc) |
paul@99 | 65 | if "-tb" in args: |
paul@99 | 66 | raise |
paul@37 | 67 | else: |
paul@62 | 68 | i.vacuum() |
paul@62 | 69 | ot = i.get_object_table() |
paul@62 | 70 | pt = i.get_parameter_table() |
paul@0 | 71 | |
paul@0 | 72 | # vim: tabstop=4 expandtab shiftwidth=4 |