1.1 --- a/micropython/__init__.py Mon Nov 10 23:57:05 2008 +0100
1.2 +++ b/micropython/__init__.py Tue Dec 02 01:13:19 2008 +0100
1.3 @@ -206,13 +206,12 @@
1.4 paramtable = self.get_parameter_table()
1.5 self.finalise()
1.6
1.7 - image = []
1.8 + self.code = []
1.9
1.10 # Append constants to the image.
1.11
1.12 for pos, const in enumerate(self.constants()):
1.13 - const.location = pos
1.14 - image.append(const)
1.15 + self.code.append(const)
1.16
1.17 last_module = self.modules_ordered[-1]
1.18
1.19 @@ -221,22 +220,21 @@
1.20 if not with_builtins and module.name == "__builtins__":
1.21 continue
1.22
1.23 - pos = len(image)
1.24 + pos = len(self.code)
1.25
1.26 # Position the module in the image and make a translation.
1.27
1.28 - module.location = pos
1.29 trans = micropython.ast.Translation(module, self)
1.30
1.31 # Add header details.
1.32
1.33 - image.append(module)
1.34 + self.code.append(module)
1.35 pos += 1
1.36
1.37 # Append module attributes to the image.
1.38
1.39 attributes = module.module_attributes()
1.40 - image += module.attributes_as_list()
1.41 + self.code += module.attributes_as_list()
1.42 pos += len(attributes.keys())
1.43
1.44 # Append classes and functions to the image.
1.45 @@ -244,28 +242,23 @@
1.46 for obj in module.all_objects:
1.47 if isinstance(obj, micropython.inspect.Class):
1.48
1.49 - # Position the class in the image.
1.50 -
1.51 - obj.location = pos
1.52 -
1.53 # Add header details.
1.54
1.55 - image.append(obj)
1.56 + self.code.append(obj)
1.57 pos += 1
1.58
1.59 # Append class attributes to the image.
1.60
1.61 attributes = obj.class_attributes()
1.62 - image += obj.attributes_as_list()
1.63 + self.code += obj.attributes_as_list()
1.64 pos += len(attributes.keys())
1.65
1.66 # Generate the instantiator/initialiser.
1.67 # Append the function code to the image.
1.68
1.69 instantiator = obj.get_instantiator()
1.70 - instantiator.code_location = pos
1.71 code = trans.get_instantiator_code(obj)
1.72 - image += code
1.73 + self.code += code
1.74 pos += len(code)
1.75
1.76 # Class-level code is generated separately at the module
1.77 @@ -274,44 +267,160 @@
1.78
1.79 elif isinstance(obj, micropython.inspect.Function):
1.80
1.81 - # Position the function in the image.
1.82 -
1.83 - obj.location = pos
1.84 -
1.85 # Add header details.
1.86
1.87 - image.append(obj)
1.88 + self.code.append(obj)
1.89 pos += 1
1.90
1.91 # Append any default values to the image.
1.92 # Only do this for named functions (not lambdas).
1.93
1.94 if obj.name is not None:
1.95 - image += obj.default_attrs
1.96 + self.code += obj.default_attrs
1.97 pos += len(obj.default_attrs)
1.98
1.99 # Append the function code to the image.
1.100
1.101 - obj.code_location = pos
1.102 code = trans.get_code(obj)
1.103 - image += code
1.104 + self.code += code
1.105 pos += len(code)
1.106
1.107 - # Remember the position of the module code.
1.108 -
1.109 - module.code_location = pos
1.110 -
1.111 # Append the module top-level code to the image.
1.112
1.113 code = trans.get_module_code(final=(module is last_module))
1.114 - image += code
1.115 + self.code += code
1.116 pos += len(code)
1.117
1.118 - # Remember the generated code and the location of the first instruction.
1.119 + return self.code
1.120 +
1.121 + def get_raw_image(self, with_builtins=0):
1.122 +
1.123 + "Return the raw image representation of the program."
1.124 +
1.125 + self.get_image(with_builtins)
1.126 +
1.127 + objtable = self.get_object_table()
1.128 + paramtable = self.get_parameter_table()
1.129 +
1.130 + # Position the objects.
1.131 +
1.132 + pos = 0
1.133 + for item in self.code:
1.134 + if isinstance(item, Block):
1.135 + item.location = pos
1.136 + pos += len(item.code)
1.137 + elif isinstance(item, (micropython.data.Class, micropython.data.Const,
1.138 + micropython.data.Function, micropython.data.Module)):
1.139 + item.location = pos
1.140 + pos += 1
1.141 + if isinstance(item, micropython.data.Function):
1.142 + item.code_location = pos
1.143 + else:
1.144 + pos += 1
1.145 +
1.146 + # Generate the raw code.
1.147 +
1.148 + self.raw_code = []
1.149 +
1.150 + for item in self.code:
1.151 +
1.152 + if isinstance(item, micropython.data.Attr):
1.153 + self.raw_code.append((
1.154 + item.context and item.context.location,
1.155 + item.value and item.value.location # no useful context is provided
1.156 + ))
1.157 +
1.158 + elif isinstance(item, Block):
1.159 + self.raw_code += self.raw_block(item, len(self.raw_code))
1.160 +
1.161 + # Using classcode, attrcode, codeaddr, codedetails, instance.
1.162 +
1.163 + elif isinstance(item, micropython.data.Class):
1.164 + assert item.location == len(self.raw_code)
1.165 +
1.166 + # NOTE: The instantiator code is the first block of the class.
1.167 +
1.168 + instantiator_code_location = item.get_instantiator().blocks[0].location
1.169 +
1.170 + # NOTE: Need initialiser details!
1.171 + self.raw_code.append((
1.172 + objtable.as_list().get_code(item.full_name()),
1.173 + objtable.get_index(item.full_name()),
1.174 + instantiator_code_location,
1.175 + (
1.176 + len(item.get_instantiator().positional_names),
1.177 + len(item.get_instantiator().defaults)
1.178 + ),
1.179 + 0
1.180 + ))
1.181
1.182 - self.code = image
1.183 + elif isinstance(item, micropython.data.Const):
1.184 + assert item.location == len(self.raw_code)
1.185 +
1.186 + # NOTE: Need class details!
1.187 + self.raw_code.append((
1.188 + objtable.as_list().get_code(item.value_type_name()),
1.189 + objtable.get_index(item.value_type_name()),
1.190 + None,
1.191 + None,
1.192 + 1
1.193 + ))
1.194 +
1.195 + elif isinstance(item, micropython.data.Function):
1.196 + assert item.location == len(self.raw_code)
1.197 +
1.198 + # NOTE: Need class and parameter details! Should arguably be types.FunctionType.
1.199 + self.raw_code.append((
1.200 + objtable.as_list().get_code("__builtins__.function"),
1.201 + objtable.get_index("__builtins__.function"),
1.202 + item.code_location,
1.203 + (
1.204 + len(item.positional_names),
1.205 + len(item.defaults)
1.206 + ),
1.207 + 0
1.208 + ))
1.209 +
1.210 + assert item.code_location == len(self.raw_code)
1.211 +
1.212 + elif isinstance(item, micropython.data.Module):
1.213 + assert item.location == len(self.raw_code)
1.214 +
1.215 + self.raw_code.append((
1.216 + objtable.as_list().get_code(item.full_name()),
1.217 + None, # module name not used as an attribute
1.218 + None,
1.219 + None,
1.220 + 0
1.221 + ))
1.222 +
1.223 + else:
1.224 + self.raw_code.append(item)
1.225 +
1.226 + # Fix the module locations.
1.227 +
1.228 + for module in self.modules_ordered:
1.229 +
1.230 + if not with_builtins and module.name == "__builtins__":
1.231 + continue
1.232 +
1.233 + module.code_location = module.blocks[0].location
1.234 +
1.235 self.code_location = self.modules["__main__"].code_location
1.236 - return image
1.237 + return self.raw_code
1.238 +
1.239 + def raw_block(self, block, location):
1.240 +
1.241 + """
1.242 + Return the code for the given 'block', appearing at the given
1.243 + 'location'.
1.244 + """
1.245 +
1.246 + assert block.location == location
1.247 + for i, item in enumerate(block.code):
1.248 + if hasattr(item, "location"):
1.249 + item.location = location + i
1.250 + return block.code
1.251
1.252 def get_object_table(self):
1.253
2.1 --- a/micropython/ast.py Mon Nov 10 23:57:05 2008 +0100
2.2 +++ b/micropython/ast.py Tue Dec 02 01:13:19 2008 +0100
2.3 @@ -128,6 +128,7 @@
2.4 self.new_op(Return())
2.5
2.6 self.unit.temp_usage = self.max_temp_position + 1
2.7 + self.unit.blocks = self.blocks
2.8 return self.blocks
2.9
2.10 def get_code(self, unit):
2.11 @@ -144,6 +145,7 @@
2.12 self.dispatch(unit.astnode)
2.13
2.14 self.unit.temp_usage = self.max_temp_position + 1
2.15 + self.unit.blocks = self.blocks
2.16 return self.blocks
2.17
2.18 def get_instantiator_code(self, cls):
2.19 @@ -183,6 +185,7 @@
2.20 self.new_op(StoreResult())
2.21 self.new_op(Return())
2.22
2.23 + self.unit.blocks = self.blocks
2.24 return self.blocks
2.25
2.26 # Allocation-related methods.
2.27 @@ -271,25 +274,6 @@
2.28 def drop_exception_blocks(self):
2.29 self.exception_blocks.pop()
2.30
2.31 - def new_label(self):
2.32 -
2.33 - "Return a new label object for use with set_label."
2.34 -
2.35 - number = self.label_number
2.36 - label = Label(number)
2.37 - self.labels[label] = label
2.38 - self.label_number += 1
2.39 - return label
2.40 -
2.41 - def set_label(self, label):
2.42 -
2.43 - """
2.44 - Set the location of 'label' to that within the entire image: the
2.45 - location within the code combined with location of the code unit.
2.46 - """
2.47 -
2.48 - label.location = len(self.code) + self.unit.code_location
2.49 -
2.50 # Assignment expression values.
2.51
2.52 def record_value(self, immediate=1):
2.53 @@ -1637,7 +1621,6 @@
2.54
2.55 unit = self.unit
2.56 self.unit = node.unit
2.57 - self.unit.code_location = self.module.code_location # class body code is not independently addressable
2.58 self.dispatch(node.code)
2.59 self.unit = unit
2.60
3.1 --- a/micropython/data.py Mon Nov 10 23:57:05 2008 +0100
3.2 +++ b/micropython/data.py Tue Dec 02 01:13:19 2008 +0100
3.3 @@ -404,6 +404,7 @@
3.4
3.5 # Program-related details.
3.6
3.7 + self.blocks = None
3.8 self.temp_usage = 0
3.9 self.local_usage = 0
3.10 self.all_local_usage = 0
3.11 @@ -749,6 +750,7 @@
3.12
3.13 # Program-related details.
3.14
3.15 + self.blocks = None
3.16 self.temp_usage = 0
3.17 self.local_usage = 0
3.18 self.all_local_usage = 0
3.19 @@ -943,6 +945,7 @@
3.20
3.21 # Program-related details.
3.22
3.23 + self.blocks = None
3.24 self.temp_usage = 0
3.25 self.local_usage = 0
3.26 self.all_local_usage = 0
4.1 --- a/micropython/rsvp.py Mon Nov 10 23:57:05 2008 +0100
4.2 +++ b/micropython/rsvp.py Tue Dec 02 01:13:19 2008 +0100
4.3 @@ -19,92 +19,7 @@
4.4 this program. If not, see <http://www.gnu.org/licenses/>.
4.5 """
4.6
4.7 -from micropython.common import Block
4.8 -from micropython.data import Attr, Class, Const, Function, Module
4.9 -
4.10 -def raw(code, objtable, paramtable):
4.11 -
4.12 - """
4.13 - Return the raw image representation of the given 'code', using the given
4.14 - 'objtable' and 'paramtable' to populate structures.
4.15 - """
4.16 -
4.17 - new_code = []
4.18 -
4.19 - for item in code:
4.20 -
4.21 - if isinstance(item, Attr):
4.22 - new_code.append((
4.23 - item.context and item.context.location,
4.24 - item.value and item.value.location # no useful context is provided
4.25 - ))
4.26 -
4.27 - elif isinstance(item, Block):
4.28 - new_code += raw_block(item, len(new_code))
4.29 -
4.30 - # Using classcode, attrcode, codeaddr, codedetails, instance.
4.31 -
4.32 - elif isinstance(item, Class):
4.33 - # NOTE: Need initialiser details!
4.34 - new_code.append((
4.35 - objtable.as_list().get_code(item.full_name()),
4.36 - objtable.get_index(item.full_name()),
4.37 - item.get_instantiator().code_location,
4.38 - (
4.39 - len(item.get_instantiator().positional_names),
4.40 - len(item.get_instantiator().defaults)
4.41 - ),
4.42 - 0
4.43 - ))
4.44 -
4.45 - elif isinstance(item, Const):
4.46 - # NOTE: Need class details!
4.47 - new_code.append((
4.48 - objtable.as_list().get_code(item.value_type_name()),
4.49 - objtable.get_index(item.value_type_name()),
4.50 - None,
4.51 - None,
4.52 - 1
4.53 - ))
4.54 -
4.55 - elif isinstance(item, Function):
4.56 - # NOTE: Need class and parameter details! Should arguably be types.FunctionType.
4.57 - new_code.append((
4.58 - objtable.as_list().get_code("__builtins__.function"),
4.59 - objtable.get_index("__builtins__.function"),
4.60 - item.code_location,
4.61 - (
4.62 - len(item.positional_names),
4.63 - len(item.defaults)
4.64 - ),
4.65 - 0
4.66 - ))
4.67 -
4.68 - elif isinstance(item, Module):
4.69 - new_code.append((
4.70 - objtable.as_list().get_code(item.full_name()),
4.71 - None, # module name not used as an attribute
4.72 - None,
4.73 - None,
4.74 - 0
4.75 - ))
4.76 -
4.77 - else:
4.78 - new_code.append(item)
4.79 -
4.80 - return new_code
4.81 -
4.82 -def raw_block(block, location):
4.83 -
4.84 - """
4.85 - Return the code for the given 'block', appearing at the given location.
4.86 - """
4.87 -
4.88 - block.location = location
4.89 - for i, item in enumerate(block.code):
4.90 - if hasattr(item, "location"):
4.91 - item.location = location + i
4.92 - return block.code
4.93 +from micropython.data import Attr
4.94
4.95 def name(attr):
4.96 if isinstance(attr, Attr):
5.1 --- a/test.py Mon Nov 10 23:57:05 2008 +0100
5.2 +++ b/test.py Tue Dec 02 01:13:19 2008 +0100
5.3 @@ -1,7 +1,6 @@
5.4 #!/usr/bin/env python
5.5
5.6 import micropython
5.7 -from micropython.rsvp import raw
5.8 from micropython.graph import get_graph
5.9 import rsvp
5.10 import sys
5.11 @@ -17,6 +16,9 @@
5.12 global code
5.13 code = importer.get_image(with_builtins)
5.14
5.15 +def raw(importer, with_builtins=0):
5.16 + return importer.get_raw_image()
5.17 +
5.18 def show_code(code):
5.19 for i, x in enumerate(code):
5.20 print i, x
5.21 @@ -38,7 +40,7 @@
5.22 attr_error = objlist.access("__builtins__", "AttributeError").value.location
5.23 type_error = objlist.access("__builtins__", "TypeError").value.location
5.24 print "Getting raw image..."
5.25 - rc = raw(importer.get_image(), ot, pt)
5.26 + rc = importer.get_raw_image()
5.27 print "Initialising the machine..."
5.28 rm = rsvp.RSVPMachine(rc, objlist.as_raw(), paramlist.as_raw(), attr_error, type_error, debug=debug)
5.29 rm.pc = importer.code_location
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/tests/attributes1.py Tue Dec 02 01:13:19 2008 +0100
6.3 @@ -0,0 +1,18 @@
6.4 +#!/usr/bin/env python
6.5 +
6.6 +class C:
6.7 + clsattr = 123
6.8 +
6.9 + def __init__(self, value):
6.10 + self.instattr = value
6.11 + self.clsattr
6.12 +
6.13 + def update(self, value):
6.14 + self.attr = value
6.15 + C.clsattr
6.16 +
6.17 +C
6.18 +C.clsattr
6.19 +C(456).update(789)
6.20 +
6.21 +# vim: tabstop=4 expandtab shiftwidth=4