1.1 --- a/micropython/__init__.py Sun Feb 22 22:31:21 2009 +0100
1.2 +++ b/micropython/__init__.py Sat Feb 28 01:49:33 2009 +0100
1.3 @@ -260,26 +260,26 @@
1.4 for item in self.code:
1.5
1.6 if isinstance(item, micropython.data.Attr):
1.7 - self.raw_code += item.as_raw(objtable)
1.8 + self.raw_code += item.as_raw(objtable, paramtable)
1.9
1.10 elif isinstance(item, Block):
1.11 assert item.location == len(self.raw_code)
1.12 - self.raw_code += item.as_raw(objtable)
1.13 + self.raw_code += item.as_raw(objtable, paramtable)
1.14
1.15 # Using classcode, attrcode, codeaddr, codedetails, instance.
1.16
1.17 elif isinstance(item, micropython.data.Class):
1.18 assert item.instance_template_location == len(self.raw_code)
1.19 - self.raw_code += item.as_raw(objtable)
1.20 + self.raw_code += item.as_raw(objtable, paramtable)
1.21 assert item.location == len(self.raw_code) - 1
1.22
1.23 elif isinstance(item, micropython.data.Const):
1.24 assert item.location == len(self.raw_code)
1.25 - self.raw_code += item.as_raw(objtable)
1.26 + self.raw_code += item.as_raw(objtable, paramtable)
1.27
1.28 elif isinstance(item, micropython.data.Function):
1.29 assert item.location == len(self.raw_code)
1.30 - self.raw_code += item.as_raw(objtable)
1.31 + self.raw_code += item.as_raw(objtable, paramtable)
1.32
1.33 # Check the code location only where the code has been generated.
1.34
1.35 @@ -288,7 +288,7 @@
1.36
1.37 elif isinstance(item, micropython.data.Module):
1.38 assert item.location == len(self.raw_code)
1.39 - self.raw_code += item.as_raw(objtable)
1.40 + self.raw_code += item.as_raw(objtable, paramtable)
1.41
1.42 else:
1.43 self.raw_code.append(item)
2.1 --- a/micropython/ast.py Sun Feb 22 22:31:21 2009 +0100
2.2 +++ b/micropython/ast.py Sat Feb 28 01:49:33 2009 +0100
2.3 @@ -770,14 +770,19 @@
2.4 continue
2.5
2.6 # Generate code for the keyword and the positioning
2.7 - # operation.
2.8 + # operation. Get the value as the source of the assignment.
2.9
2.10 self.dispatch(arg.expr)
2.11 + self.record_value()
2.12 +
2.13 + # Store the source value using the callable's parameter
2.14 + # table information.
2.15 +
2.16 + self.new_op(temp)
2.17 self.new_op(StoreFrameIndex(paramindex))
2.18
2.19 - # use (callable+0)+paramindex+table
2.20 - # checks embedded offset against (callable+0)
2.21 - # moves the current value to frame+position
2.22 + self.set_source()
2.23 + self.discard_value()
2.24
2.25 # Record the highest possible frame position for this argument.
2.26
3.1 --- a/micropython/common.py Sun Feb 22 22:31:21 2009 +0100
3.2 +++ b/micropython/common.py Sat Feb 28 01:49:33 2009 +0100
3.3 @@ -3,7 +3,7 @@
3.4 """
3.5 Common classes.
3.6
3.7 -Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk>
3.8 +Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk>
3.9
3.10 This program is free software; you can redistribute it and/or modify it under
3.11 the terms of the GNU General Public License as published by the Free Software
3.12 @@ -84,10 +84,7 @@
3.13 def __repr__(self):
3.14 return "Block(%r, location=%r)" % (id(self), self.location)
3.15
3.16 - def as_raw(self, objtable):
3.17 -
3.18 - "Return the code for the given 'block'."
3.19 -
3.20 + def as_raw(self, objtable, paramtable):
3.21 for i, item in enumerate(self.code):
3.22 if hasattr(item, "location"):
3.23 item.location = location + i
3.24 @@ -99,16 +96,17 @@
3.25
3.26 "A representation of a raw program data object."
3.27
3.28 - def __init__(self, classcode, attrcode, codeaddr, codedetails, instance, name):
3.29 + def __init__(self, classcode, attrcode, codeaddr, codedetails, instance, name, funccode=None):
3.30 self.classcode = classcode
3.31 self.attrcode = attrcode
3.32 self.codeaddr = codeaddr
3.33 self.codedetails = codedetails
3.34 self.instance = instance
3.35 self.name = name
3.36 + self.funccode = funccode
3.37
3.38 def __repr__(self):
3.39 - return "%r # %s" % ((self.classcode, self.attrcode, self.codeaddr, self.codedetails, self.instance), self.name)
3.40 + return "%r # %s" % ((self.classcode, self.attrcode, self.codeaddr, self.codedetails, self.instance, self.funccode), self.name)
3.41
3.42 # Inspection representations.
3.43
4.1 --- a/micropython/data.py Sun Feb 22 22:31:21 2009 +0100
4.2 +++ b/micropython/data.py Sat Feb 28 01:49:33 2009 +0100
4.3 @@ -20,7 +20,7 @@
4.4
4.5 --------
4.6
4.7 -The central classes in this module are the following:
4.8 +The central data structure classes in this module are the following:
4.9
4.10 * Class
4.11 * Function
4.12 @@ -34,7 +34,7 @@
4.13 access names within their defined scopes. Specific methods also exist in order
4.14 to distinguish between certain kinds of attributes:
4.15
4.16 - * Class: (class|all_class|instance|all)_attributes
4.17 + * Class: class_attributes, all_class_attributes, instance_attributes, all_attributes
4.18 * Function: parameters, locals, all_locals
4.19 * Module: module_attributes
4.20
4.21 @@ -101,7 +101,7 @@
4.22 self.module.set(name, value, 0)
4.23 else:
4.24 attr = self._set(name, value)
4.25 - attr.update(attr.value, single_assignment)
4.26 + attr.update(value, single_assignment)
4.27
4.28 def set_module(self, name, value):
4.29
4.30 @@ -310,7 +310,7 @@
4.31 self.name, shortrepr(self.value), self.assignments
4.32 )
4.33
4.34 - def as_raw(self, objtable):
4.35 + def as_raw(self, objtable, paramtable):
4.36 return [
4.37 (
4.38 self.context and self.context.location,
4.39 @@ -365,7 +365,7 @@
4.40
4.41 __shortrepr__ = __repr__
4.42
4.43 - def as_raw(self, objtable):
4.44 + def as_raw(self, objtable, paramtable):
4.45 # NOTE: Need class details!
4.46 return [
4.47 DataObject(
4.48 @@ -461,7 +461,7 @@
4.49 def __shortrepr__(self):
4.50 return "Class(%r, %s)" % (self.name, shortrepr(self.parent))
4.51
4.52 - def as_raw(self, objtable):
4.53 + def as_raw(self, objtable, paramtable):
4.54 classcode = objtable.as_list().get_code(self.full_name())
4.55 attrcode = objtable.get_index(self.full_name())
4.56
4.57 @@ -861,7 +861,7 @@
4.58 self.name, shortrepr(self.parent)
4.59 )
4.60
4.61 - def as_raw(self, objtable):
4.62 + def as_raw(self, objtable, paramtable):
4.63 # NOTE: Need class and parameter details! Should arguably be types.FunctionType.
4.64 return [
4.65 DataObject(
4.66 @@ -873,7 +873,8 @@
4.67 len(self.defaults)
4.68 ),
4.69 0,
4.70 - "__builtins__.function"
4.71 + "__builtins__.function",
4.72 + paramtable.as_list().get_code(self.full_name())
4.73 )
4.74 ]
4.75
4.76 @@ -1065,7 +1066,7 @@
4.77 def __shortrepr__(self):
4.78 return "Module(%r)" % self.name
4.79
4.80 - def as_raw(self, objtable):
4.81 + def as_raw(self, objtable, paramtable):
4.82 return [
4.83 DataObject(
4.84 objtable.as_list().get_code(self.full_name()),
5.1 --- a/micropython/opt.py Sun Feb 22 22:31:21 2009 +0100
5.2 +++ b/micropython/opt.py Sat Feb 28 01:49:33 2009 +0100
5.3 @@ -172,7 +172,7 @@
5.4
5.5 """
5.6 Return whether 'instruction' can use simple input from the current
5.7 - value. Such instructions would, in a low-level implementation, be able
5.8 + value. Such instructions would, in a low-level implementation, be able
5.9 to have the simple input registers as operands.
5.10 """
5.11
5.12 @@ -180,6 +180,7 @@
5.13 StoreTemp, StoreFrame, StoreResult, StoreException, # as the value being stored
5.14 LoadAddressContext, LoadAttr, LoadAttrIndex, # as the object being referenced
5.15 StoreAttr, StoreAttrIndex, StoreCallable, # as the object being referenced
5.16 + StoreFrameIndex, # as the object being referenced
5.17 LoadCallable,
5.18 TestIdentity, TestIdentityAddress, CheckSelf, # as one of the operands
5.19 CheckException, CheckFrame, MakeObject,
6.1 --- a/micropython/rsvp.py Sun Feb 22 22:31:21 2009 +0100
6.2 +++ b/micropython/rsvp.py Sat Feb 28 01:49:33 2009 +0100
6.3 @@ -163,7 +163,7 @@
6.4 class LoadAttr(AR): "Load into the current value the given attribute of the object referenced by the current value."
6.5 class StoreAttr(AR): "Store the source value into the given attribute of the object referenced by the current value."
6.6 class LoadAttrIndex(Immediate): "Load into the current value the attribute of the current value with the given index."
6.7 -class StoreAttrIndex(Immediate): "Store an object in the attribute with the given index."
6.8 +class StoreAttrIndex(Immediate): "Store the source value into the attribute of the current value with the given index."
6.9
6.10 # Access to object details.
6.11
6.12 @@ -176,7 +176,7 @@
6.13 class DropFrame(Instruction): "Drop an invocation frame."
6.14 class RecoverFrame(Instruction): "Recover the current frame as an invocation frame."
6.15 class StoreFrame(Immediate): "Store the current value as an argument for the parameter with the given position."
6.16 -class StoreFrameIndex(Immediate): "Store the current value as an argument for the parameter with the given index."
6.17 +class StoreFrameIndex(Immediate): "Store the source value as an argument of the current value for the parameter with the given index."
6.18 class LoadContext(Instruction): "Load the context of an invocation."
6.19 class CheckFrame(Immediate): "Check the invocation frame and context for the target."
6.20 class CheckSelf(Instruction): "Check the first argument of an invocation against the target."
7.1 --- a/micropython/table.py Sun Feb 22 22:31:21 2009 +0100
7.2 +++ b/micropython/table.py Sat Feb 28 01:49:33 2009 +0100
7.3 @@ -193,6 +193,7 @@
7.4 "A parameter list."
7.5
7.6 def entry_as_raw(self, entry):
7.7 + # param_index, position = entry
7.8 return entry
7.9
7.10 class Table:
8.1 --- a/rsvp.py Sun Feb 22 22:31:21 2009 +0100
8.2 +++ b/rsvp.py Sat Feb 28 01:49:33 2009 +0100
8.3 @@ -273,9 +273,11 @@
8.4 instructions would otherwise have.
8.5 """
8.6
8.7 + value = self.value
8.8 if self.instruction.source is not None:
8.9 self.perform(self.instruction.source)
8.10 self.source = self.value
8.11 + self.value = value
8.12 if self.instruction.input is not None:
8.13 self.perform(self.instruction.input)
8.14
8.15 @@ -324,7 +326,7 @@
8.16
8.17 def StoreAddress(self):
8.18 # Preserve context.
8.19 - self.save(self.operand, self.value)
8.20 + self.save(self.operand, self.source)
8.21
8.22 def MakeObject(self):
8.23 size = self.operand
8.24 @@ -362,6 +364,7 @@
8.25 self.value = self.load(ref + offset)
8.26 else:
8.27 self.exception = self.attr_error
8.28 + return self.RaiseException()
8.29
8.30 def StoreAttrIndex(self):
8.31 context, ref = self.value
8.32 @@ -371,10 +374,12 @@
8.33 if attr_index == self.operand:
8.34 if class_attr:
8.35 self.exception = self.type_error
8.36 + return self.RaiseException()
8.37 else:
8.38 self.save(ref + offset, self.source)
8.39 else:
8.40 self.exception = self.attr_error
8.41 + return self.RaiseException()
8.42
8.43 # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden.
8.44
8.45 @@ -398,12 +403,14 @@
8.46 context, ref = self.value
8.47 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame
8.48 data = self.load(ref)
8.49 - element = self.objlist[data.classcode + self.operand]
8.50 - attr_index, class_attr, replace_context, offset = element
8.51 - if attr_index == self.operand:
8.52 - self.frame_stack[frame + offset] = self.value
8.53 + element = self.paramlist[data.funccode + self.operand]
8.54 + # NOTE: Need to ensure correct positioning where a context has been generated.
8.55 + param_index, offset = element
8.56 + if param_index == self.operand:
8.57 + self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated
8.58 else:
8.59 self.exception = self.type_error
8.60 + return self.RaiseException()
8.61
8.62 def LoadCallable(self):
8.63 context, ref = self.value
8.64 @@ -437,11 +444,24 @@
8.65 self.invocation_sp_stack[-1] += 1
8.66 operand -= 1
8.67
8.68 + # Test the frame size.
8.69 +
8.70 nargs, ndefaults = data.codedetails
8.71 if not ((nargs - ndefaults) <= operand <= nargs):
8.72 raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, operand, nargs)
8.73
8.74 - # NOTE: Support population of defaults.
8.75 + # Support population of defaults.
8.76 + # This involves copying the "attributes" of a function into the frame.
8.77 +
8.78 + default = operand - (nargs - ndefaults)
8.79 + self.frame_stack.extend([None] * (nargs - operand))
8.80 + pos = self.operand
8.81 +
8.82 + while operand < nargs:
8.83 + self.frame_stack[frame + pos] = self.load(ref + default + 1) # skip header
8.84 + default += 1
8.85 + pos += 1
8.86 + operand += 1
8.87
8.88 def CheckSelf(self):
8.89 context, ref = self.value
9.1 --- a/tests/call_func_default.py Sun Feb 22 22:31:21 2009 +0100
9.2 +++ b/tests/call_func_default.py Sat Feb 28 01:49:33 2009 +0100
9.3 @@ -1,9 +1,9 @@
9.4 #!/usr/bin/env python
9.5
9.6 def f(a, b, c=4):
9.7 - pass
9.8 + return c
9.9
9.10 -f(1, 2, 3)
9.11 -f(1, 2)
9.12 +r1 = f(1, 2, 3)
9.13 +r2 = f(1, 2)
9.14
9.15 # vim: tabstop=4 expandtab shiftwidth=4
10.1 --- a/tests/call_func_default_redefine.py Sun Feb 22 22:31:21 2009 +0100
10.2 +++ b/tests/call_func_default_redefine.py Sat Feb 28 01:49:33 2009 +0100
10.3 @@ -1,16 +1,16 @@
10.4 #!/usr/bin/env python
10.5
10.6 def f(a, b, c=4):
10.7 - pass
10.8 + return c
10.9
10.10 g = f
10.11 -g(1, c=3, b=2)
10.12 -g(1, 2)
10.13 +r1 = g(1, c=3, b=2)
10.14 +r2 = g(1, 2)
10.15
10.16 def g(a, c, b=5):
10.17 - pass
10.18 + return b
10.19
10.20 -g(1, c=3, b=2)
10.21 -g(1, 3)
10.22 +r3 = g(1, c=3, b=2)
10.23 +r4 = g(1, 3)
10.24
10.25 # vim: tabstop=4 expandtab shiftwidth=4