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(program, with_builtins=0): 12 make(program, with_builtins) 13 show_code(code) 14 15 def make(program, with_builtins=0): 16 global code 17 code = program.get_image(with_builtins) 18 19 def raw(program, with_builtins=0): 20 return program.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(program, with_builtins=0, debug=0): 33 print "Making the image..." 34 make(program, with_builtins) 35 print "Getting raw structures..." 36 ot = program.get_object_table() 37 pt = program.get_parameter_table() 38 objlist = ot.as_list() 39 paramlist = pt.as_list() 40 print "Getting raw image..." 41 rc = program.get_raw_image() 42 print "Initialising the machine..." 43 rm = rsvp.RSVPMachine(rc, objlist, paramlist, debug=debug) 44 rm.pc = program.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.Program.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, "-v" in args, requested_optimisations) 71 p = micropython.Program(i, requested_optimisations) 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 ot = p.get_object_table() 87 pt = p.get_parameter_table() 88 89 if "-m" in args: 90 rm = machine(p) 91 92 # vim: tabstop=4 expandtab shiftwidth=4