1.1 --- a/rsvp.py Sun May 24 20:28:56 2009 +0200
1.2 +++ b/rsvp.py Mon May 25 00:45:53 2009 +0200
1.3 @@ -248,11 +248,15 @@
1.4
1.5 "Execute code in the memory, starting from the current PC address."
1.6
1.7 + breakpoint = 0
1.8 +
1.9 try:
1.10 while 1:
1.11 self.execute()
1.12 except EmptyPCStack:
1.13 pass
1.14 + except BreakpointReached:
1.15 + breakpoint = 1
1.16
1.17 print "Execution terminated",
1.18 if self.exception is not None:
1.19 @@ -260,9 +264,38 @@
1.20 addr = self.load(ref + 1)
1.21 print "with exception:", self.load(ref)
1.22 print "At address %d: %r" % (addr, self.load(addr))
1.23 + elif breakpoint:
1.24 + print "with breakpoint."
1.25 + print "At address", self.pc
1.26 else:
1.27 print "successfully."
1.28
1.29 + def test(self, module):
1.30 +
1.31 + """
1.32 + Test the code in the memory by running the code and investigating the
1.33 + contents of variables. Use 'module' to identify result variables.
1.34 + """
1.35 +
1.36 + self.run()
1.37 + success = 1
1.38 +
1.39 + for name in module.keys():
1.40 + if name.startswith("result"):
1.41 + label, expected = name.split("_")
1.42 + attr = module[name]
1.43 +
1.44 + # NOTE: Assumptions about headers and content made.
1.45 +
1.46 + attr_location = module.location + 1 + attr.position
1.47 + context, ref = self.load(attr_location)
1.48 + content = self.load(ref + 1)
1.49 + print label, expected, content
1.50 +
1.51 + success = success and (int(expected) == content)
1.52 +
1.53 + return success
1.54 +
1.55 def execute(self):
1.56
1.57 "Execute code in the memory at the current PC address."
1.58 @@ -837,4 +870,24 @@
1.59 "__builtins__.object.__init__" : builtins_object_init,
1.60 }
1.61
1.62 +# Convenience functions.
1.63 +
1.64 +def machine(program, with_builtins=0, debug=0):
1.65 + print "Making the image..."
1.66 + code = program.get_image(with_builtins)
1.67 + print "Getting raw structures..."
1.68 + ot = program.get_object_table()
1.69 + pt = program.get_parameter_table()
1.70 + objlist = ot.as_list()
1.71 + paramlist = pt.as_list()
1.72 + print "Getting raw image..."
1.73 + rc = program.get_raw_image()
1.74 + print "Initialising the machine..."
1.75 + importer = program.get_importer()
1.76 + true_constant = importer.get_constant(True).location
1.77 + false_constant = importer.get_constant(False).location
1.78 + rm = RSVPMachine(rc, objlist, paramlist, true_constant, false_constant, debug=debug)
1.79 + rm.pc = program.code_location
1.80 + return rm
1.81 +
1.82 # vim: tabstop=4 expandtab shiftwidth=4