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 attr_error = objlist.access("__builtins__", "AttributeError").value.location 41 type_error = objlist.access("__builtins__", "TypeError").value.location 42 print "Getting raw image..." 43 rc = program.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 = program.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.Program.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, "-v" in args, requested_optimisations) 73 p = micropython.Program(i, requested_optimisations) 74 75 try: 76 builtins = i.load_from_file("lib/builtins.py", "__builtins__") 77 if program is None: 78 print "Loading module micropython ..." 79 m = i.load("micropython") 80 else: 81 print "Loading program", program, "..." 82 m = i.load_from_file(program) 83 except micropython.ProcessingError, exc: 84 print repr(exc) 85 if "-tb" in args: 86 raise 87 else: 88 ot = p.get_object_table() 89 pt = p.get_parameter_table() 90 91 # vim: tabstop=4 expandtab shiftwidth=4