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