1 #!/usr/bin/env python 2 3 import micropython 4 from micropython.rsvp import raw 5 import rsvp 6 import sys 7 import os 8 9 code = None 10 11 def show(importer, with_builtins=0, optimisations=None): 12 make(importer, with_builtins, optimisations) 13 show_code(code) 14 15 def make(importer, with_builtins=0, optimisations=None): 16 optimisations = optimisations or requested_optimisations 17 global code 18 code = importer.get_image(with_builtins, optimisations) 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, optimisations=None): 31 print "Making the image..." 32 make(importer, with_builtins, optimisations) 33 print "Getting raw structures..." 34 ot = importer.get_object_table() 35 pt = importer.get_parameter_table() 36 objlist = ot.as_raw() 37 paramlist = pt.as_raw() 38 rc = raw(importer.get_image(), ot, pt) 39 print "Initialising the machine..." 40 rm = rsvp.RSVPMachine(rc, objlist, paramlist) 41 rm.pc = importer.code_location 42 return rm 43 44 def attrs(obj): 45 for name, attr in obj.items(): 46 print name, attr 47 48 if __name__ == "__main__": 49 args = sys.argv[2:] 50 path = sys.path[:] 51 52 if len(sys.argv) > 1: 53 program = os.path.abspath(sys.argv[1]) 54 path.append(os.path.split(program)[0]) 55 else: 56 program = None 57 58 i = micropython.Importer(path, "-v" in args) 59 60 if "-omax" in args: 61 requested_optimisations = i.supported_optimisations 62 else: 63 requested_optimisations = [] 64 for arg in args: 65 if arg.startswith("-o"): 66 for arg_part in arg[2:].split(","): 67 requested_optimisations.append(arg_part) 68 69 try: 70 builtins = i.load_from_file("lib/builtins.py", "__builtins__") 71 if program is None: 72 print "Loading module micropython ..." 73 m = i.load("micropython") 74 else: 75 print "Loading program", program, "..." 76 m = i.load_from_file(program) 77 except micropython.ProcessingError, exc: 78 print repr(exc) 79 if "-tb" in args: 80 raise 81 else: 82 i.vacuum() 83 ot = i.get_object_table() 84 pt = i.get_parameter_table() 85 86 # vim: tabstop=4 expandtab shiftwidth=4