# HG changeset patch # User Paul Boddie # Date 1205027217 -3600 # Node ID b830830c253490b83bda4afa5c218384fbcd919c # Parent ea87a87f49b911e63c2a9a10c4836ac465f1559d Fixed module placement when generating an image. Reverted usage of Const around functions and classes. Introduced specific classes of instructions. Fixed parameter positioning. Fixed single assignment optimisation output for attribute access. Restricted attribute finalisation to classes and functions. diff -r ea87a87f49b9 -r b830830c2534 micropython/__init__.py --- a/micropython/__init__.py Sun Mar 09 02:10:12 2008 +0100 +++ b/micropython/__init__.py Sun Mar 09 02:46:57 2008 +0100 @@ -100,11 +100,6 @@ pos = len(image) - # Position the module in the image and make a translation. - - module.location = pos - trans = micropython.ast.Translation(module, objtable, paramtable) - # Append constants to the image. for const in module.constants(): @@ -112,6 +107,11 @@ image.append(const) pos += 1 + # Position the module in the image and make a translation. + + module.location = pos + trans = micropython.ast.Translation(module, objtable, paramtable) + # Add header details. image.append(module) @@ -126,12 +126,11 @@ # Append classes and functions to the image. for obj in module.all_objects: - - # Fix the attributes. + if isinstance(obj, micropython.inspect.Class): - obj.finalise_attributes() + # Fix the attributes. - if isinstance(obj, micropython.inspect.Class): + obj.finalise_attributes() # Position the class in the image. @@ -156,6 +155,10 @@ elif isinstance(obj, micropython.inspect.Function): + # Fix the attributes. + + obj.finalise_attributes() + # Position the function in the image. obj.location = pos diff -r ea87a87f49b9 -r b830830c2534 micropython/ast.py --- a/micropython/ast.py Sun Mar 09 02:10:12 2008 +0100 +++ b/micropython/ast.py Sun Mar 09 02:46:57 2008 +0100 @@ -181,7 +181,7 @@ target_name = target.full_name() table_entry = self.objtable.table[target_name] pos = table_entry[attrname] - self.replace_op(AttrInstruction(pos + target.location)) + self.replace_op(AttrInstruction(pos)) # Where the last operation involves the special 'self' name, check to # see if the attribute is acceptably positioned. @@ -324,7 +324,7 @@ self.new_op(LoadContext()) self.new_op(CheckContext()) self.new_op(JumpIfTrue(continue_label)) - self.new_op(LoadConst("TypeError")) # NOTE: Do this properly! + self.new_op(LoadConst(Const("TypeError"))) # NOTE: Do this properly! self.new_op(RaiseException()) self.set_label(continue_label) @@ -503,7 +503,7 @@ # Only store the name when visiting this node from outside. if self.unit is not node.unit: - self.new_op(LoadConst(Const(node.unit))) + self.new_op(LoadConst(node.unit)) self._visitName(node, (StoreName, StoreAttr)) # Visiting of the code occurs when get_code is invoked on this node. diff -r ea87a87f49b9 -r b830830c2534 micropython/inspect.py --- a/micropython/inspect.py Sun Mar 09 02:10:12 2008 +0100 +++ b/micropython/inspect.py Sun Mar 09 02:46:57 2008 +0100 @@ -257,11 +257,10 @@ class Const: - "A constant object with no parent defined as its context." + "A constant object with no context." def __init__(self, value): self.value = value - self.parent = None # Image generation details. @@ -656,7 +655,7 @@ self[name].position = i j = i - for i, attr in enumerate(self.locals()): + for i, attr in enumerate(self.locals().values()): attr.position = i + j class UnresolvedName(NamespaceDict): diff -r ea87a87f49b9 -r b830830c2534 micropython/rsvp.py --- a/micropython/rsvp.py Sun Mar 09 02:10:12 2008 +0100 +++ b/micropython/rsvp.py Sun Mar 09 02:46:57 2008 +0100 @@ -34,24 +34,58 @@ else: return "%s()" % self.__class__.__name__ +class StackRelativeInstruction(Instruction): + + "An instruction operating on the local value stack." + + def __repr__(self): + return "%s(%r)" % (self.__class__.__name__, self.attr.position) + +SR = StackRelativeInstruction + +class AddressRelativeInstruction(Instruction): + + "An instruction accessing an object's attribute." + + def __repr__(self): + position = self.attr.position + if isinstance(self.attr.parent, Instance): + location = "instance" + result = position + else: + location = self.attr.parent.location + result = location + position + 1 + return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) + +AR = AddressRelativeInstruction + +class ImmediateInstruction(Instruction): + + "An instruction loading an address directly." + + def __repr__(self): + return "%s(%r)" % (self.__class__.__name__, self.attr.location) + +Immediate = ImmediateInstruction + # Instructions operating on the value stack. -class LoadConst(Instruction): "Load the constant from the specified location." +class LoadConst(Immediate): "Load the constant from the specified location." class Duplicate(Instruction): "Duplicate the top of stack." class Pop(Instruction): "Pop the top of stack." # Access within an invocation frame. -class LoadName(Instruction): "Load the object from the given local attribute/variable." -class StoreName(Instruction): "Store the object in the given local attribute/variable." +class LoadName(SR): "Load the object from the given local attribute/variable." +class StoreName(SR): "Store the object in the given local attribute/variable." # Access to address-relative data. class MakeObject(Instruction): "Make a new object." # ... DropObject not defined: Assume garbage collection. -class LoadAttr(Instruction): "Load the object from the given attribute." +class LoadAttr(AR): "Load the object from the given attribute." +class StoreAttr(AR): "Store an object in the given attribute." class LoadAttrIndex(Instruction): "Load the object for the attribute with the given index." -class StoreAttr(Instruction): "Store an object in the given attribute." class StoreAttrIndex(Instruction): "Store an object in the attribute with the given index." # Access to invocation frames in preparation. diff -r ea87a87f49b9 -r b830830c2534 test.py --- a/test.py Sun Mar 09 02:10:12 2008 +0100 +++ b/test.py Sun Mar 09 02:46:57 2008 +0100 @@ -3,8 +3,8 @@ import micropython import sys -def show(importer): - for i, x in enumerate(importer.get_image()): +def show(importer, with_builtins=0): + for i, x in enumerate(importer.get_image(with_builtins=with_builtins)): print i, x def attrs(obj):