1.1 --- a/micropython/rsvp.py Sun May 18 19:59:26 2008 +0200
1.2 +++ b/micropython/rsvp.py Sun May 18 20:50:05 2008 +0200
1.3 @@ -21,6 +21,20 @@
1.4
1.5 from micropython.data import Attr, Const, Instance
1.6
1.7 +def raw(code):
1.8 + raw_code = []
1.9 + old_to_new = {}
1.10 + new_i = 0
1.11 + for i, obj in enumerate(code):
1.12 + if not isinstance(obj, Instruction):
1.13 + raw_code.append(obj)
1.14 + else:
1.15 + old_to_new[i] = new_i
1.16 + new_raw_code = obj.to_raw()
1.17 + raw_code += new_raw_code
1.18 + new_i += len(new_raw_code)
1.19 + return raw_code
1.20 +
1.21 class Instruction:
1.22
1.23 "A generic instruction."
1.24 @@ -36,6 +50,9 @@
1.25 else:
1.26 return "%s()" % self.__class__.__name__
1.27
1.28 + def to_raw(self):
1.29 + return [self.__class__.__name__]
1.30 +
1.31 class StackRelativeInstruction(Instruction):
1.32
1.33 "An instruction operating on the local value stack."
1.34 @@ -43,17 +60,28 @@
1.35 def __repr__(self):
1.36 return "%s(%r)" % (self.__class__.__name__, self.attr.position)
1.37
1.38 + def get_operands(self):
1.39 + return self.attr.position
1.40 +
1.41 + def to_raw(self):
1.42 + position = self.get_operands()
1.43 + return [self.__class__.__name__, position]
1.44 +
1.45 SR = StackRelativeInstruction
1.46
1.47 class AddressRelativeInstruction(Instruction):
1.48
1.49 - "An instruction accessing an object's attribute."
1.50 + "An instruction accessing an instance's attribute."
1.51
1.52 def __repr__(self):
1.53 - position = self.attr.position
1.54 - location = "instance"
1.55 - result = position
1.56 - return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result)
1.57 + return "%s(%r)" % (self.__class__.__name__, self.attr.position)
1.58 +
1.59 + def get_operands(self):
1.60 + return self.attr.position
1.61 +
1.62 + def to_raw(self):
1.63 + position = self.get_operands()
1.64 + return [self.__class__.__name__, position]
1.65
1.66 AR = AddressRelativeInstruction
1.67
1.68 @@ -62,6 +90,13 @@
1.69 "An instruction loading an address directly."
1.70
1.71 def __repr__(self):
1.72 + location, position, result = self.get_operands()
1.73 + if position is not None and result is not None:
1.74 + return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result)
1.75 + else:
1.76 + return "%s(%r)" % (self.__class__.__name__, location)
1.77 +
1.78 + def get_operands(self):
1.79 if isinstance(self.attr, Attr):
1.80 position = self.attr.position
1.81 location = self.attr.parent.location
1.82 @@ -74,9 +109,16 @@
1.83 location = self.attr.parent.name
1.84 position = self.attr.name
1.85 result = None
1.86 - return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result)
1.87 + return location, position, result
1.88 else:
1.89 - return "%s(%r)" % (self.__class__.__name__, self.attr.location)
1.90 + return self.attr.location, None, None
1.91 +
1.92 + def to_raw(self):
1.93 + location, position, result = self.get_operands()
1.94 + if position is not None and result is not None:
1.95 + return [self.__class__.__name__, result]
1.96 + else:
1.97 + return [self.__class__.__name__, location]
1.98
1.99 Address = AddressInstruction
1.100
1.101 @@ -87,6 +129,9 @@
1.102 def __repr__(self):
1.103 return "%s(%r)" % (self.__class__.__name__, self.attr)
1.104
1.105 + def to_raw(self):
1.106 + return [self.__class__.__name__, self.attr]
1.107 +
1.108 Immediate = ImmediateInstruction
1.109
1.110 # Mix-in classes for stack effects.