# HG changeset patch # User Paul Boddie # Date 1243184583 -7200 # Node ID 44279e9276d6d31cb367c6b3ea1f701956478002 # Parent be4035baf424981183874b6c3ca5e973f37c10d7 Removed the LoadAttrIndexContext instruction. Made LoadFunction usable for various optimisations. Split various tests into smaller, more specific versions. diff -r be4035baf424 -r 44279e9276d6 micropython/ast.py --- a/micropython/ast.py Sun May 24 18:12:07 2009 +0200 +++ b/micropython/ast.py Sun May 24 19:03:03 2009 +0200 @@ -37,11 +37,11 @@ attribute_load_instructions = ( LoadAddress, LoadAddressContext, LoadAddressContextCond, - LoadAttr, LoadAttrIndex, LoadAttrIndexContext, LoadAttrIndexContextCond + LoadAttr, LoadAttrIndex, LoadAttrIndexContextCond ) attribute_store_instructions = ( None, None, None, - StoreAttr, StoreAttrIndex, None, None + StoreAttr, StoreAttrIndex, None ) # Name access instructions, for use with the appropriate handlers. diff -r be4035baf424 -r 44279e9276d6 micropython/opt.py --- a/micropython/opt.py Sun May 24 18:12:07 2009 +0200 +++ b/micropython/opt.py Sun May 24 19:03:03 2009 +0200 @@ -149,7 +149,7 @@ "Return whether 'instruction' provides a constant input." return isinstance(instruction, LoadAddress) and instruction.attr.assignments == 1 or \ - isinstance(instruction, LoadConst) + isinstance(instruction, (LoadConst, LoadFunction)) def is_constant_target(self, instruction): @@ -169,7 +169,7 @@ a load operation from a CPU register or special memory location. """ - return isinstance(instruction, (LoadConst, LoadName, LoadTemp, LoadResult, LoadException, LoadAddress)) + return isinstance(instruction, (LoadConst, LoadFunction, LoadName, LoadTemp, LoadResult, LoadException, LoadAddress)) def is_simple_input_user(self, instruction): @@ -182,7 +182,7 @@ return isinstance(instruction, ( StoreTemp, StoreFrame, StoreResult, StoreException, # as the value being stored LoadAddressContext, LoadAddressContextCond, # as the object being referenced - LoadAttr, LoadAttrIndex, LoadAttrIndexContext, # as the object being referenced + LoadAttr, LoadAttrIndex, # LoadAttrIndexContext, # as the object being referenced LoadAttrIndexContextCond, # as the object being referenced StoreAttr, StoreAttrIndex, StoreCallable, # as the object being referenced StoreFrameIndex, # as the object being referenced @@ -270,7 +270,7 @@ # LoadResult cannot be relied upon in general since the result register # could be updated since first being referenced. - return isinstance(self.active_value, (LoadName, LoadTemp, LoadAddress, LoadConst)) or \ + return isinstance(self.active_value, (LoadName, LoadTemp, LoadAddress, LoadConst, LoadFunction)) or \ isinstance(self.active_value, LoadResult) and self.active_value is self.active or \ isinstance(self.active_value, LoadException) and self.active_value is self.active diff -r be4035baf424 -r 44279e9276d6 micropython/rsvp.py --- a/micropython/rsvp.py Sun May 24 18:12:07 2009 +0200 +++ b/micropython/rsvp.py Sun May 24 19:03:03 2009 +0200 @@ -181,9 +181,7 @@ class StoreAttr(AR): "Store the source value into the given attribute of the object referenced by the current value." class LoadAttrIndex(Immediate): "Load into the current value the attribute of the current value with the given index." class StoreAttrIndex(Immediate): "Store the source value into the attribute of the current value with the given index." -class LoadAttrIndexContext(Immediate): - """Load into the current value the attribute of the current value with the given index, making the - current value the context.""" +##### LoadAttrIndexContext not defined. class LoadAttrIndexContextCond(Immediate): """Load into the current value the attribute of the current value with the given index, only making the current value the context if the attribute is compatible.""" @@ -243,11 +241,12 @@ class InvertBoolean(Instruction): "Invert the boolean status." # Instructions which affect the current value. +# LoadAttrIndexContext not defined. current_value_instructions = ( - LoadConst, LoadName, LoadTemp, + LoadConst, LoadFunction, LoadName, LoadTemp, LoadAddress, LoadAddressContext, LoadAddressContextCond, - LoadAttr, LoadAttrIndex, LoadAttrIndexContext, LoadAttrIndexContextCond, + LoadAttr, LoadAttrIndex, LoadAttrIndexContextCond, LoadCallable, LoadContext, LoadResult, LoadException, MakeObject ) diff -r be4035baf424 -r 44279e9276d6 micropython/trans.py --- a/micropython/trans.py Sun May 24 18:12:07 2009 +0200 +++ b/micropython/trans.py Sun May 24 19:03:03 2009 +0200 @@ -318,8 +318,7 @@ """ AddressInstruction, AddressContextInstruction, AddressContextCondInstruction, \ - AttrInstruction, AttrIndexInstruction, AttrIndexContextInstruction, \ - AttrIndexContextCondInstruction = classes + AttrInstruction, AttrIndexInstruction, AttrIndexContextCondInstruction = classes # Where the last operation (defining the attribute owner) yields a # constant... diff -r be4035baf424 -r 44279e9276d6 rsvp.py --- a/rsvp.py Sun May 24 18:12:07 2009 +0200 +++ b/rsvp.py Sun May 24 19:03:03 2009 +0200 @@ -429,17 +429,7 @@ self.exception = self._MakeObject(2, self.attr_error_instance) return self.RaiseException() - def LoadAttrIndexContext(self): - context, ref = self.value - data = self.load(ref) - element = self.objlist[data.classcode + self.operand] - attr_index, class_attr, offset = element - if attr_index == self.operand: - loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute - self.value = ref, loaded_ref - else: - self.exception = self._MakeObject(2, self.attr_error_instance) - return self.RaiseException() + # LoadAttrIndexContext not defined. def LoadAttrIndexContextCond(self): context, ref = self.value diff -r be4035baf424 -r 44279e9276d6 tests/attributes2.py --- a/tests/attributes2.py Sun May 24 18:12:07 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#!/usr/bin/env python - -def e(x): pass - -class C: - e = e - - def test(self): - self.e() - -class D: - e = C.e - - def test(self): - self.e(1) # TypeError: unbound C.e needs C instance, not int - -class E(C): - # e = C.e (via inheritance) - - def test(self): - self.e() - -c = C() -d = D() -e = E() - -p = c.e # bound C.e -q = d.e # unbound C.e -r = e.e # bound E.e - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r be4035baf424 -r 44279e9276d6 tests/attributes3.py --- a/tests/attributes3.py Sun May 24 18:12:07 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -#!/usr/bin/env python - -class C: - def g(self): - return self.f # C.f with context self - def f(self): pass - - def test(self): - self.f() - self.g() - -class D: - f = C.f - def g(self): - return self.f # D.f with context preserved - - def test(self): - self.f(2) - self.g() - -class E(C): - def g(self): - return self.f # C.f with context self - - def test(self): - self.f() - self.g() - -C.f -D.f -E.f - -c = C() -d = D() -e = E() - -x = c.f # bound C.f == c.g() -y = d.f # unbound C.f == d.g() -z = e.f # bound E.f == e.g() - -# vim: tabstop=4 expandtab shiftwidth=4 diff -r be4035baf424 -r 44279e9276d6 tests/attributes_class_bind_function_inherited_internal.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_class_bind_function_inherited_internal.py Sun May 24 19:03:03 2009 +0200 @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +def e(self, x): + return x + +class C: + e = e + +class E(C): + # e = C.e (via inheritance) + + def test(self, x): + return self.e(x) + +e = E() +result = e.e(321) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r be4035baf424 -r 44279e9276d6 tests/attributes_class_bind_function_inherited_via_self.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_class_bind_function_inherited_via_self.py Sun May 24 19:03:03 2009 +0200 @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +def e(self, x): + return x + +class C: + e = e + +class E(C): + # e = C.e (via inheritance) + + def f(self): + return self.e + +e = E() +r = e.f() # bound E.e +result = r(321) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r be4035baf424 -r 44279e9276d6 tests/attributes_class_bind_function_internal.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_class_bind_function_internal.py Sun May 24 19:03:03 2009 +0200 @@ -0,0 +1,15 @@ +#!/usr/bin/env python + +def e(self, x): + return x + +class C: + e = e + + def test(self, x): + return self.e(x) + +c = C() +result = c.test(321) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r be4035baf424 -r 44279e9276d6 tests/attributes_class_bind_function_via_self.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes_class_bind_function_via_self.py Sun May 24 19:03:03 2009 +0200 @@ -0,0 +1,16 @@ +#!/usr/bin/env python + +def e(self, x): + return x + +class C: + e = e + + def f(self): + return self.e + +c = C() +p = c.f() # bound C.e +result = p(321) + +# vim: tabstop=4 expandtab shiftwidth=4