# HG changeset patch # User Paul Boddie # Date 1210379540 -7200 # Node ID 619142384781e8b5f603058f76750a283645b3f5 # Parent 5790cc867f93d2fcd1518c5e01cbba66349699e1 Made AddressRelativeInstruction only display instance-related details. Changed LoadAddress and StoreAddress to use AddressInstruction, which can now display Attr and other relevant objects. Removed None from the builtins module, making it a special constant. Changed module inspection so that many handlers return instances instead of None, thus producing better attributes for namespace entries. Made constants per program rather than per module. Fixed function default attributes for instantiators. Fixed optimised attribute storage involving LoadConst. diff -r 5790cc867f93 -r 619142384781 README.txt --- a/README.txt Mon May 05 00:46:38 2008 +0200 +++ b/README.txt Sat May 10 02:32:20 2008 +0200 @@ -30,7 +30,7 @@ The fundamental "value type" is a pair of references: one pointing to the referenced object represented by the interchangeable value; one referring to the context of the referenced object, typically the object through which the -referenced object was acquired as an attribute.A +referenced object was acquired as an attribute. Value Layout ------------ diff -r 5790cc867f93 -r 619142384781 lib/builtins.py --- a/lib/builtins.py Mon May 05 00:46:38 2008 +0200 +++ b/lib/builtins.py Sat May 10 02:32:20 2008 +0200 @@ -260,18 +260,12 @@ # Various types. class EllipsisType(object): pass - -class NoneType(object): - def __bool__(self): pass - def __str__(self): pass - class NotImplementedType: pass # Special values. True = bool() False = bool() -None = NoneType() Ellipsis = EllipsisType() NotImplemented = NotImplementedType() diff -r 5790cc867f93 -r 619142384781 micropython/__init__.py --- a/micropython/__init__.py Mon May 05 00:46:38 2008 +0200 +++ b/micropython/__init__.py Sat May 10 02:32:20 2008 +0200 @@ -72,6 +72,20 @@ self.objtable = None self.paramtable = None + # Constant records. + + self.constant_values = {} + self.constant_list = None # cache for constants + + def constants(self): + + "Return a list of constants." + + if self.constant_list is None: + self.constant_list = list(self.constant_values.values()) + + return self.constant_list + def vacuum(self): "Tidy up the modules." @@ -97,6 +111,12 @@ image = [] + # Append constants to the image. + + for pos, const in enumerate(self.constants()): + const.location = pos + image.append(const) + for module_name, module in self.modules.items(): if not with_builtins and module_name == "__builtins__": continue @@ -107,13 +127,6 @@ pos = len(image) - # Append constants to the image. - - for const in module.constants(): - const.location = pos - image.append(const) - pos += 1 - # Position the module in the image and make a translation. module.location = pos diff -r 5790cc867f93 -r 619142384781 micropython/ast.py --- a/micropython/ast.py Mon May 05 00:46:38 2008 +0200 +++ b/micropython/ast.py Sat May 10 02:32:20 2008 +0200 @@ -264,12 +264,15 @@ # Get the details of the access. - target = last.attr.value + if isinstance(last.attr, micropython.inspect.Const): + target_name = last.attr.value_type_name() + else: + target = last.attr.value - if isinstance(target, micropython.inspect.Const): - target_name = target.value_type_name() - else: - target_name = target.full_name() + if isinstance(target, micropython.inspect.Const): + target_name = target.value_type_name() + else: + target_name = target.full_name() # Access the object table to get the attribute position. @@ -1064,7 +1067,11 @@ self._visitBinary(node, "__mul__", "__rmul__") def visitName(self, node): - self._visitName(node, (LoadName, LoadAddress)) + if node.name == "None": + const = self.module.constant_values[None] + self.new_op(LoadConst(const)) + else: + self._visitName(node, (LoadName, LoadAddress)) def visitNot(self, node): pass diff -r 5790cc867f93 -r 619142384781 micropython/inspect.py --- a/micropython/inspect.py Mon May 05 00:46:38 2008 +0200 +++ b/micropython/inspect.py Sat May 10 02:32:20 2008 +0200 @@ -616,6 +616,8 @@ del self.positional_names[-1] # Initialise default storage. + # NOTE: This must be initialised separately due to the reliance on node + # NOTE: visiting. self.default_attrs = [] @@ -743,8 +745,10 @@ "Make a function from a method." - return Function(self.name, self.parent, self.argnames[1:], self.defaults, + function = Function(self.name, self.parent, self.argnames[1:], self.defaults, self.has_star, self.has_dstar, self.global_namespace, self.node) + function.default_attrs = self.default_attrs + return function class UnresolvedName(NamespaceDict): @@ -793,11 +797,6 @@ self.all_objects = set() - # Constant records. - - self.constant_values = {} - self.constant_list = None # cache for constants - # Keyword records. self.keyword_names = set() @@ -834,15 +833,6 @@ return dict(self) - def constants(self): - - "Return a list of constants." - - if self.constant_list is None: - self.constant_list = list(self.constant_values.values()) - - return self.constant_list - # Program visitors. class InspectedModule(ASTVisitor, Module): @@ -867,6 +857,10 @@ self.builtins = self.importer.modules.get("__builtins__") self.loaded = 0 + # Constant records. + + self.constant_values = importer.constant_values + # Current expression state. self.expr = None @@ -957,9 +951,14 @@ self.dispatch(n) return None - visitAdd = NOP + def OP(self, node): + for n in node.getChildNodes(): + self.dispatch(n) + return Instance() - visitAnd = NOP + visitAdd = OP + + visitAnd = OP visitAssert = NOP @@ -989,23 +988,24 @@ visitAssTuple = visitAssList - visitAugAssign = NOP + visitAugAssign = OP - visitBackquote = NOP + visitBackquote = OP - visitBitand = NOP + visitBitand = OP - visitBitor = NOP + visitBitor = OP - visitBitxor = NOP + visitBitxor = OP visitBreak = NOP - visitCallFunc = NOP + visitCallFunc = OP def visitClass(self, node): if self.namespaces: print "Class %r in %r is not global: ignored." % (node.name, self.namespaces[-1].full_name()) + return None else: cls = Class(node.name, self.get_parent(), self, node) @@ -1045,9 +1045,9 @@ self.dispatch(node.code) self.namespaces.pop() - return None + return cls - visitCompare = NOP + visitCompare = OP def visitConst(self, node): const = Const(node.value) @@ -1058,24 +1058,25 @@ visitDecorators = NOP - visitDict = NOP + visitDict = OP visitDiscard = NOP - visitDiv = NOP + visitDiv = OP visitEllipsis = NOP visitExec = NOP - visitExpression = NOP + visitExpression = OP - visitFloorDiv = NOP + visitFloorDiv = OP def visitFor(self, node): self.in_loop = 1 self.NOP(node) self.in_loop = 0 + return None def visitFrom(self, node): module = self.importer.load(node.modname, 1) @@ -1145,9 +1146,9 @@ self.namespaces.pop() self.store(node.name, function) - return None + return function - visitGenExpr = NOP + visitGenExpr = OP visitGenExprFor = NOP @@ -1196,7 +1197,7 @@ return None - visitInvert = NOP + visitInvert = OP def visitKeyword(self, node): self.dispatch(node.expr) @@ -1205,28 +1206,32 @@ self.keyword_names.add(node.name) return None - visitLambda = NOP + visitLambda = OP - visitLeftShift = NOP + visitLeftShift = OP - visitList = NOP + visitList = OP - visitListComp = NOP + visitListComp = OP visitListCompFor = NOP visitListCompIf = NOP - visitMod = NOP + visitMod = OP def visitModule(self, node): return self.dispatch(node.node) - visitMul = NOP + visitMul = OP def visitName(self, node): name = node.name - if self.namespaces and self.namespaces[-1].has_key(name): + if name == "None": + const = Const(None) + self.constant_values[None] = const + return const + elif self.namespaces and self.namespaces[-1].has_key(name): return self.namespaces[-1][name] elif self.has_key(name): return self[name] @@ -1235,13 +1240,13 @@ else: return None - visitNot = NOP + visitNot = OP - visitOr = NOP + visitOr = OP visitPass = NOP - visitPower = NOP + visitPower = OP visitPrint = NOP @@ -1251,20 +1256,20 @@ visitReturn = NOP - visitRightShift = NOP + visitRightShift = OP - visitSlice = NOP + visitSlice = OP - visitSliceobj = NOP + visitSliceobj = OP def visitStmt(self, node): for n in node.nodes: self.dispatch(n) return None - visitSub = NOP + visitSub = OP - visitSubscript = NOP + visitSubscript = OP def visitTryExcept(self, node): self.dispatch(node.body) @@ -1276,16 +1281,17 @@ visitTryFinally = NOP - visitTuple = NOP + visitTuple = OP - visitUnaryAdd = NOP + visitUnaryAdd = OP - visitUnarySub = NOP + visitUnarySub = OP def visitWhile(self, node): self.in_loop = 1 self.NOP(node) self.in_loop = 0 + return None visitWith = NOP diff -r 5790cc867f93 -r 619142384781 micropython/rsvp.py --- a/micropython/rsvp.py Mon May 05 00:46:38 2008 +0200 +++ b/micropython/rsvp.py Sat May 10 02:32:20 2008 +0200 @@ -19,7 +19,7 @@ this program. If not, see . """ -from micropython.inspect import Const, Instance +from micropython.inspect import Attr, Const, Instance class Instruction: @@ -51,10 +51,19 @@ def __repr__(self): position = self.attr.position - if isinstance(self.attr.parent, Instance): - location = "instance" - result = position - else: + location = "instance" + result = position + return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) + +AR = AddressRelativeInstruction + +class AddressInstruction(Instruction): + + "An instruction loading an address directly." + + def __repr__(self): + if isinstance(self.attr, Attr): + position = self.attr.position location = self.attr.parent.location # NOTE: Unpositioned attributes are handled here. @@ -65,16 +74,9 @@ location = self.attr.parent.name position = self.attr.name result = None - return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) - -AR = AddressRelativeInstruction - -class AddressInstruction(Instruction): - - "An instruction loading an address directly." - - def __repr__(self): - return "%s(%r)" % (self.__class__.__name__, self.attr.location) + return "%s(%r, %r -> %r)" % (self.__class__.__name__, location, position, result) + else: + return "%s(%r)" % (self.__class__.__name__, self.attr.location) Address = AddressInstruction @@ -112,7 +114,7 @@ # Instructions operating on the value stack. -class LoadConst(StackAdd, Address): "Load the constant from the specified location." +class LoadConst(StackAdd, Address): "Load the constant, class, function, module from the specified location." class Duplicate(StackAdd, Instruction): "Duplicate the top of the stack." class Pop(StackRemove, Immediate): "Pop entries from the top of the stack." @@ -131,8 +133,8 @@ class StoreAttr(StackRemove2, AR): "Store an object in the given attribute." class LoadAttrIndex(Immediate): "Load the object for the attribute with the given index." class StoreAttrIndex(StackRemove2, Immediate): "Store an object in the attribute with the given index." -class LoadAddress(StackAdd, AR): "Load the object from the given fixed attribute address." -class StoreAddress(StackRemove, AR): "Store an object in the given fixed attribute address." +class LoadAddress(StackAdd, Address): "Load the object from the given fixed attribute address." +class StoreAddress(StackRemove, Address): "Store an object in the given fixed attribute address." # Access to invocation frames in preparation.