# HG changeset patch # User Paul Boddie # Date 1205025012 -3600 # Node ID ea87a87f49b911e63c2a9a10c4836ac465f1559d # Parent 129e39a901170bceea8ddd8e0f24d1904a399a5a Added an Instance class to indicate where instance accesses are being performed. Extended the Const class to hold empty parent/context details. Fixed function local name positions. Added a method to Function to determine whether an attribute is relocated for the class owning the function. Reverted the parameter initialisation "fix" (set_parameter) since parameters are unlikely to be single assignment candidates. Added explicit attribute finalisation in the image generation. Added a tentative optimisation for instance attributes (related to the is_relocated method mentioned above). Added incompatibility notes. diff -r 129e39a90117 -r ea87a87f49b9 docs/compatibility.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/docs/compatibility.txt Sun Mar 09 02:10:12 2008 +0100 @@ -0,0 +1,27 @@ +This document describes various compatibility differences between micropython +and other Python implementations. + +Class Attribute Assignment +-------------------------- + + Python micropython + ------ ----------- + +class C: + ... + +# cls refers to C + +cls.attr = value Class attribute Run-time error + assigned + +Module Attribute Assignment +--------------------------- + + Python micropython + ------ ----------- + +import somemod + +somemod.attr = value Module attribute Run-time error + assigned diff -r 129e39a90117 -r ea87a87f49b9 micropython/__init__.py --- a/micropython/__init__.py Tue Mar 04 00:41:53 2008 +0100 +++ b/micropython/__init__.py Sun Mar 09 02:10:12 2008 +0100 @@ -94,6 +94,10 @@ if not with_builtins and module_name == "__builtins__": continue + # Fix the attributes. + + module.finalise_attributes() + pos = len(image) # Position the module in the image and make a translation. @@ -122,6 +126,11 @@ # Append classes and functions to the image. for obj in module.all_objects: + + # Fix the attributes. + + obj.finalise_attributes() + if isinstance(obj, micropython.inspect.Class): # Position the class in the image. diff -r 129e39a90117 -r ea87a87f49b9 micropython/ast.py --- a/micropython/ast.py Tue Mar 04 00:41:53 2008 +0100 +++ b/micropython/ast.py Sun Mar 09 02:10:12 2008 +0100 @@ -170,6 +170,9 @@ # NOTE: Only simple cases are used for optimisations. last = self.last_op() + + # Where the last operation yields a constant... + if isinstance(last, (LoadName, LoadAttr)) and last.attr.assignments == 1: # Get the details of the access. @@ -178,7 +181,16 @@ target_name = target.full_name() table_entry = self.objtable.table[target_name] pos = table_entry[attrname] - self.replace_op(AttrInstruction(pos)) + self.replace_op(AttrInstruction(pos + target.location)) + + # Where the last operation involves the special 'self' name, check to + # see if the attribute is acceptably positioned. + + elif isinstance(last, LoadName) and last.attr.name == "self" and not self.unit.is_relocated(attrname): + attr = self.unit.parent.all_attributes()[attrname] + self.new_op(AttrInstruction(attr)) + + # Otherwise, perform a normal operation. else: index = self.objtable.get_index(attrname) @@ -491,7 +503,7 @@ # Only store the name when visiting this node from outside. if self.unit is not node.unit: - self.new_op(LoadConst(node.unit)) + self.new_op(LoadConst(Const(node.unit))) self._visitName(node, (StoreName, StoreAttr)) # Visiting of the code occurs when get_code is invoked on this node. diff -r 129e39a90117 -r ea87a87f49b9 micropython/inspect.py --- a/micropython/inspect.py Tue Mar 04 00:41:53 2008 +0100 +++ b/micropython/inspect.py Sun Mar 09 02:10:12 2008 +0100 @@ -151,10 +151,6 @@ attr.assignments = 1 attr.assignment_values.add(value) - "A specialised set operation for parameters." - - set_parameter = set_module - def _set(self, name, value): "The underlying set operation associating 'name' with 'value'." @@ -220,7 +216,7 @@ class Attr: - "An attribute entry." + "An attribute entry having a parent as context." def __init__(self, position, parent, name, value=None, assignments=None): self.position = position @@ -261,10 +257,11 @@ class Const: - "A constant object." + "A constant object with no parent defined as its context." def __init__(self, value): self.value = value + self.parent = None # Image generation details. @@ -287,6 +284,12 @@ "An inspected class." def __init__(self, name, parent, global_namespace=None, node=None): + + """ + Initialise the class with the given 'name', 'parent' object, optional + 'global_namespace' and optional AST 'node'. + """ + NamespaceDict.__init__(self, global_namespace) self.name = name self.parent = parent @@ -512,7 +515,7 @@ d = {} for i, name in enumerate(self._get_position_list(positions)): - d[name] = Attr(i, None, None, name) + d[name] = Attr(i, Instance(), name, None) return d def _cmp_positions(self, a, b): @@ -586,7 +589,7 @@ if isinstance(name, tuple): self._add_parameters(name) else: - self.set_parameter(name, None) + self.set(name, None) def __repr__(self): if self.location is not None: @@ -633,6 +636,29 @@ del self.localnames[name] return self.localnames + def is_relocated(self, name): + + """ + Determine whether the given attribute 'name' is relocated for instances + having this function as a method. + """ + + for cls in self.parent.descendants: + if name in cls.relocated: + return 1 + return 0 + + def finalise_attributes(self): + + "Make sure all attributes are fully defined." + + for i, name in enumerate(self.argnames): + self[name].position = i + + j = i + for i, attr in enumerate(self.locals()): + attr.position = i + j + class UnresolvedName(NamespaceDict): "A module, class or function which was mentioned but could not be imported." @@ -657,6 +683,13 @@ else: return self.parent_name +class Instance: + + "A placeholder indicating the involvement of an instance." + + def __repr__(self): + return "Instance()" + class Module(NamespaceDict): "An inspected module's core details." diff -r 129e39a90117 -r ea87a87f49b9 micropython/rsvp.py --- a/micropython/rsvp.py Tue Mar 04 00:41:53 2008 +0100 +++ b/micropython/rsvp.py Sun Mar 09 02:10:12 2008 +0100 @@ -3,7 +3,7 @@ """ RSVP instruction classes. -Copyright (C) 2007 Paul Boddie +Copyright (C) 2007, 2008 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -19,6 +19,8 @@ this program. If not, see . """ +from micropython.inspect import Const, Instance + class Instruction: "A generic instruction."