# HG changeset patch # User Paul Boddie # Date 1211136605 -7200 # Node ID fed8371e233a45071b288c7b6ed4e97e0c6f3ffd # Parent e83bc6d2e3b40e38dffa2d92e0fafa7f5bdaa855 Added partial support for converting generated code into a "raw" format, although this may not be particularly appropriate at this stage of development. diff -r e83bc6d2e3b4 -r fed8371e233a micropython/rsvp.py --- a/micropython/rsvp.py Sun May 18 19:59:26 2008 +0200 +++ b/micropython/rsvp.py Sun May 18 20:50:05 2008 +0200 @@ -21,6 +21,20 @@ from micropython.data import Attr, Const, Instance +def raw(code): + raw_code = [] + old_to_new = {} + new_i = 0 + for i, obj in enumerate(code): + if not isinstance(obj, Instruction): + raw_code.append(obj) + else: + old_to_new[i] = new_i + new_raw_code = obj.to_raw() + raw_code += new_raw_code + new_i += len(new_raw_code) + return raw_code + class Instruction: "A generic instruction." @@ -36,6 +50,9 @@ else: return "%s()" % self.__class__.__name__ + def to_raw(self): + return [self.__class__.__name__] + class StackRelativeInstruction(Instruction): "An instruction operating on the local value stack." @@ -43,17 +60,28 @@ def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self.attr.position) + def get_operands(self): + return self.attr.position + + def to_raw(self): + position = self.get_operands() + return [self.__class__.__name__, position] + SR = StackRelativeInstruction class AddressRelativeInstruction(Instruction): - "An instruction accessing an object's attribute." + "An instruction accessing an instance's attribute." def __repr__(self): - position = self.attr.position - location = "instance" - result = position - return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) + return "%s(%r)" % (self.__class__.__name__, self.attr.position) + + def get_operands(self): + return self.attr.position + + def to_raw(self): + position = self.get_operands() + return [self.__class__.__name__, position] AR = AddressRelativeInstruction @@ -62,6 +90,13 @@ "An instruction loading an address directly." def __repr__(self): + location, position, result = self.get_operands() + if position is not None and result is not None: + return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) + else: + return "%s(%r)" % (self.__class__.__name__, location) + + def get_operands(self): if isinstance(self.attr, Attr): position = self.attr.position location = self.attr.parent.location @@ -74,9 +109,16 @@ location = self.attr.parent.name position = self.attr.name result = None - return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) + return location, position, result else: - return "%s(%r)" % (self.__class__.__name__, self.attr.location) + return self.attr.location, None, None + + def to_raw(self): + location, position, result = self.get_operands() + if position is not None and result is not None: + return [self.__class__.__name__, result] + else: + return [self.__class__.__name__, location] Address = AddressInstruction @@ -87,6 +129,9 @@ def __repr__(self): return "%s(%r)" % (self.__class__.__name__, self.attr) + def to_raw(self): + return [self.__class__.__name__, self.attr] + Immediate = ImmediateInstruction # Mix-in classes for stack effects. diff -r e83bc6d2e3b4 -r fed8371e233a rsvp.py --- a/rsvp.py Sun May 18 19:59:26 2008 +0200 +++ b/rsvp.py Sun May 18 20:50:05 2008 +0200 @@ -151,23 +151,29 @@ except IndexError: raise EmptyMetadataStack - def execute(self): + def run(self): "Execute code in the memory, starting from the current PC address." try: while 1: - instruction = self.load(self.pc) - if self.debug: - print "%8d %s" % (self.pc, instruction) - method = getattr(self, instruction, None) - if method is None: - raise IllegalInstruction, self.pc - else: - method() + self.execute() except EmptyPCStack: pass + def execute(self): + + "Execute code at the current PC address." + + instruction = self.load(self.pc) + if self.debug: + print "%8d %s" % (self.pc, instruction) + method = getattr(self, instruction, None) + if method is None: + raise IllegalInstruction, self.pc + else: + method() + def jump(self, addr, next): """ @@ -277,31 +283,28 @@ def LoadAddress(self): """ - LoadAddress addr, #n - Load from position n in reference at addr: get the contents of position - n in the memory referenced by addr, adding the retrieved value to the - top of the stack. + LoadAddress addr + Load from addr: get the contents of the location in memory referenced by + addr, adding the retrieved value to the top of the stack. """ - red = self.load(self.pc + 1) - n = self.load(self.pc + 2) - self.push(self.load(ref + n)) - self.pc += 3 + ref = self.load(self.pc + 1) + self.push(self.load(ref)) + self.pc += 2 def StoreAddress(self): """ - StoreAddress addr, #n - Save to position n in reference at addr: pull a value from the stack and - save it to position n in the memory referenced by addr, also removing - the value on the top of the stack. + StoreAddress addr + Save to addr: pull a value from the stack and save it to the location in + memory referenced by addr, also removing the value on the top of the + stack. """ ref = self.load(self.pc + 1) - n = self.load(self.pc + 2) value = self.pull() - self.save(ref + n, value) - self.pc += 3 + self.save(ref, value) + self.pc += 2 def Return(self): diff -r e83bc6d2e3b4 -r fed8371e233a test.py --- a/test.py Sun May 18 19:59:26 2008 +0200 +++ b/test.py Sun May 18 20:50:05 2008 +0200 @@ -1,6 +1,7 @@ #!/usr/bin/env python import micropython +from micropython.rsvp import raw import sys code = None @@ -9,6 +10,9 @@ optimisations = optimisations or requested_optimisations global code code = importer.get_image(with_builtins, optimisations) + show_code(code) + +def show_code(code): for i, x in enumerate(code): print i, x