# HG changeset patch # User Paul Boddie # Date 1209410382 -7200 # Node ID 3ab797077458571207d5a5ba45d108eca3cacbfc # Parent 7afa1b5f52cee1ca43991b8c9aba5e1619a5047e Added support for getting constant attributes more efficiently. diff -r 7afa1b5f52ce -r 3ab797077458 micropython/ast.py --- a/micropython/ast.py Mon Apr 28 00:53:10 2008 +0200 +++ b/micropython/ast.py Mon Apr 28 21:19:42 2008 +0200 @@ -255,10 +255,15 @@ return # Get the details of the access. - # NOTE: Support constants here. target = last.attr.value - 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. try: table_entry = self.objtable.table[target_name] @@ -272,9 +277,9 @@ raise TranslateError(self.module.full_name(), node, "No attribute entry exists for name %r in target %r." % (attrname, target_name)) - # NOTE: Support constants here. + # Produce a suitable instruction. - if isinstance(target, micropython.inspect.Instance): + if isinstance(target, micropython.inspect.Const): self.replace_op(AttrInstruction(pos)) else: self.replace_op(AddressInstruction(pos)) @@ -418,16 +423,8 @@ frame_pos = len(args) - # NOTE: If any extra keywords were identified, generate them now. - # NOTE: This needs to populate a ** argument dictionary. - # - # for arg in extra_keywords: - # const = self.module.constant_values[arg.name] - # self.new_op(LoadConst(const)) - # self.dispatch(arg.expr) - # frame_pos += 1 - - # NOTE: Somehow, the above needs to be combined with * and ** arguments. + # NOTE: Extra keywords are not supported. + # NOTE: Somehow, the above needs to be combined with * arguments. # Either test for a complete set of arguments. @@ -556,7 +553,7 @@ def _have_constant_input(self, n): last = self.last_ops(n+1) return len(last) > n and (isinstance(last[n], LoadAddress) and last[n].attr.assignments == 1 or - isinstance(last[n], LoadConst)) + isinstance(last[n], LoadConst)) # and not isinstance(last[n].attr, micropython.inspect.Instance) def _have_known_target(self): return self._have_constant_input(0) diff -r 7afa1b5f52ce -r 3ab797077458 micropython/inspect.py --- a/micropython/inspect.py Mon Apr 28 00:53:10 2008 +0200 +++ b/micropython/inspect.py Mon Apr 28 21:19:42 2008 +0200 @@ -272,12 +272,17 @@ else: return "Const(%r)" % self.value + # Support constants as dictionary keys in order to build constant tables. + def __eq__(self, other): return self.value == other.value def __hash__(self): return hash(self.value) + def value_type_name(self): + return "__builtins__." + self.value.__class__.__name__ + class Class(NamespaceDict, Naming): "An inspected class."