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