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