1.1 --- a/micropython/__init__.py Sat Oct 10 03:09:29 2009 +0200
1.2 +++ b/micropython/__init__.py Sun Oct 11 02:40:20 2009 +0200
1.3 @@ -39,8 +39,7 @@
1.4 which the functionality of the micropython package may be accessed.
1.5 """
1.6
1.7 -from micropython.common import *
1.8 -from micropython.program import Block
1.9 +from micropython.common import TableGenerationError
1.10 import micropython.ast
1.11 import micropython.data
1.12 import micropython.opt
1.13 @@ -89,7 +88,10 @@
1.14
1.15 def get_image(self, with_builtins=0):
1.16
1.17 - "Return a dictionary mapping modules to structures."
1.18 + """
1.19 + Return the program image including built-in objects if 'with_builtins'
1.20 + is specified and set to a true value.
1.21 + """
1.22
1.23 if self.code is not None:
1.24 return self.code
1.25 @@ -102,13 +104,12 @@
1.26
1.27 # Append constants to the image.
1.28
1.29 - for pos, const in enumerate(self.importer.constants()):
1.30 + for const in self.importer.constants():
1.31 self.code.append(const)
1.32
1.33 last_module = self.importer.modules_ordered[-1]
1.34
1.35 for module in self.importer.modules_ordered:
1.36 - pos = len(self.code)
1.37 suppress_builtins = not with_builtins and module.name == "__builtins__"
1.38
1.39 # Position the module in the image and make a translation.
1.40 @@ -118,13 +119,11 @@
1.41 # Add header details.
1.42
1.43 self.code.append(module)
1.44 - pos += 1
1.45
1.46 # Append module attributes to the image.
1.47
1.48 attributes = module.module_attributes()
1.49 self.code += module.attributes_as_list()
1.50 - pos += len(attributes.keys())
1.51
1.52 # Append classes and functions to the image.
1.53
1.54 @@ -134,13 +133,11 @@
1.55 # Add header details.
1.56
1.57 self.code.append(obj)
1.58 - pos += 1
1.59
1.60 # Append class attributes to the image.
1.61
1.62 attributes = obj.class_attributes()
1.63 self.code += obj.attributes_as_list()
1.64 - pos += len(attributes.keys())
1.65
1.66 # Omit built-in function code where requested.
1.67
1.68 @@ -153,7 +150,6 @@
1.69 instantiator = obj.get_instantiator()
1.70 code = trans.get_instantiator_code(obj)
1.71 self.code += code
1.72 - pos += len(code)
1.73
1.74 # Class-level code is generated separately at the module
1.75 # level, and the code location is set within the code
1.76 @@ -164,14 +160,12 @@
1.77 # Add header details.
1.78
1.79 self.code.append(obj)
1.80 - pos += 1
1.81
1.82 # Append any default values to the image.
1.83 # Only do this for named functions (not lambdas).
1.84
1.85 if obj.name is not None:
1.86 self.code += obj.default_attrs
1.87 - pos += len(obj.default_attrs)
1.88
1.89 # Omit built-in function code where requested.
1.90
1.91 @@ -183,7 +177,6 @@
1.92 else:
1.93 code = trans.get_code(obj)
1.94 self.code += code
1.95 - pos += len(code)
1.96
1.97 # Omit built-in module code where requested.
1.98
1.99 @@ -195,14 +188,15 @@
1.100 else:
1.101 code = trans.get_module_code()
1.102 self.code += code
1.103 - pos += len(code)
1.104
1.105 return self.code
1.106
1.107 - def get_raw_image(self, with_builtins=0):
1.108 + def get_raw_image(self, architecture=None, with_builtins=0):
1.109
1.110 "Return the raw image representation of the program."
1.111
1.112 + architecture = architecture or micropython.rsvp
1.113 +
1.114 self.get_image(with_builtins)
1.115
1.116 objtable = self.get_object_table()
1.117 @@ -213,56 +207,12 @@
1.118 pos = 0
1.119
1.120 for item in self.code:
1.121 -
1.122 - # Blocks are positioned leaving space for their expansion.
1.123 -
1.124 - if isinstance(item, Block):
1.125 - item.location = pos
1.126 - pos += len(item.code)
1.127 -
1.128 - # Other multi-location objects.
1.129 -
1.130 - elif isinstance(item, (
1.131 - micropython.data.Class, micropython.data.Const,
1.132 - micropython.data.Function, micropython.data.Module
1.133 - )):
1.134 -
1.135 - if isinstance(item, micropython.data.Class):
1.136 -
1.137 - # Append a template of an instance for use when
1.138 - # instantiating classes.
1.139 -
1.140 - item.instance_template_location = pos
1.141 - pos += 1
1.142 -
1.143 - # Record the location of the object.
1.144 + arch_item = architecture.get_object(item)
1.145
1.146 - item.location = pos
1.147 - pos += 1
1.148 -
1.149 - # Code and details are associated with certain objects.
1.150 -
1.151 - if isinstance(item, micropython.data.Function):
1.152 -
1.153 - # Set the code location only where the code has been
1.154 - # generated.
1.155 -
1.156 - if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None:
1.157 - item.code_location = item.full_name()
1.158 + # Get the raw version for the architecture.
1.159
1.160 - # Skip any defaults for named functions.
1.161 -
1.162 - elif item.name is not None:
1.163 - item.code_location = pos + len(item.defaults)
1.164 -
1.165 - # Skip any defaults for lambda functions.
1.166 -
1.167 - else:
1.168 - item.code_location = pos
1.169 -
1.170 - elif isinstance(item, micropython.data.Const):
1.171 - pos += len(item.raw_data())
1.172 -
1.173 + if arch_item is not None:
1.174 + pos = arch_item.set_location(pos, with_builtins)
1.175 else:
1.176 pos += 1
1.177
1.178 @@ -271,50 +221,16 @@
1.179 self.raw_code = []
1.180
1.181 for item in self.code:
1.182 -
1.183 - if isinstance(item, micropython.data.Attr):
1.184 - self.raw_code += item.as_raw(objtable, paramtable)
1.185 + arch_item = architecture.get_object(item)
1.186
1.187 - elif isinstance(item, Block):
1.188 - assert item.location == len(self.raw_code)
1.189 - self.raw_code += item.as_raw(objtable, paramtable)
1.190 -
1.191 - elif isinstance(item, micropython.data.Class):
1.192 - assert item.instance_template_location == len(self.raw_code)
1.193 - self.raw_code += item.as_raw(objtable, paramtable,
1.194 - with_builtins or item.module.name != "__builtins__" or item.astnode.doc is not None)
1.195 - assert item.location == len(self.raw_code) - 1
1.196 + # Get the raw version for the architecture.
1.197
1.198 - elif isinstance(item, micropython.data.Const):
1.199 - assert item.location == len(self.raw_code)
1.200 - self.raw_code += item.as_raw(objtable, paramtable)
1.201 -
1.202 - elif isinstance(item, micropython.data.Function):
1.203 - assert item.location == len(self.raw_code)
1.204 - self.raw_code += item.as_raw(objtable, paramtable)
1.205 -
1.206 - # Check the code location only where the code has been generated.
1.207 -
1.208 - assert (not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None) or \
1.209 - item.name is not None and item.code_location == len(self.raw_code) + len(item.defaults) or \
1.210 - item.name is None and 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 - self.raw_code += item.as_raw(objtable, paramtable)
1.215 -
1.216 + if arch_item is not None:
1.217 + self.raw_code += arch_item.as_raw(objtable, paramtable, with_builtins)
1.218 + arch_item.finalise_location(with_builtins)
1.219 else:
1.220 self.raw_code.append(item)
1.221
1.222 - # Set the code body location for items now that the code blocks have
1.223 - # been positioned.
1.224 -
1.225 - if isinstance(item, (micropython.data.Class, micropython.data.Function)):
1.226 - if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None:
1.227 - item.code_body_location = item.full_name()
1.228 - else:
1.229 - item.code_body_location = item.get_body_block().location
1.230 -
1.231 # Fix the module locations.
1.232
1.233 for module in self.importer.modules_ordered: