1.1 --- a/micropython/__init__.py Fri Jun 28 21:17:02 2013 +0200
1.2 +++ b/micropython/__init__.py Sat Jun 29 01:28:12 2013 +0200
1.3 @@ -28,10 +28,9 @@
1.4 importer = Importer(sys.path)
1.5
1.6 To generate programs, the above importer should be supplied in the
1.7 -initialisation of a program instance, and then various methods are called:
1.8 +initialisation of a program instance:
1.9
1.10 program = Program(importer)
1.11 -image = program.get_raw_image()
1.12
1.13 Such importer and program objects are the most convenient mechanism through
1.14 which the functionality of the micropython package may be accessed.
1.15 @@ -40,14 +39,9 @@
1.16 from micropython.data import *
1.17 from micropython.errors import *
1.18 from micropython.objectset import ObjectSet
1.19 -from micropython.program import Location
1.20 from micropython.types import *
1.21 -import micropython.ast
1.22 -import micropython.native
1.23 -import micropython.opt
1.24 import micropython.inspect
1.25 import micropython.table
1.26 -import bisect
1.27 import os
1.28 import sys
1.29
1.30 @@ -60,32 +54,20 @@
1.31
1.32 "This class supports the generation of a program image."
1.33
1.34 - supported_optimisations = micropython.opt.Optimiser.supported_optimisations
1.35 -
1.36 - def __init__(self, importer, optimisations=None):
1.37 + def __init__(self, importer):
1.38
1.39 """
1.40 Initialise the program representation with an 'importer' which is able
1.41 to locate and load Python modules.
1.42 -
1.43 - The optional 'optimisations' cause certain techniques to be used in
1.44 - reducing program size and improving program efficiency.
1.45 """
1.46
1.47 self.importer = importer
1.48 - self.optimisations = optimisations or set()
1.49 - self.native = micropython.native.NativeLibrary(self)
1.50
1.51 # Remember the tables once generated.
1.52
1.53 self.objtable = None
1.54 self.paramtable = None
1.55
1.56 - # Main program information.
1.57 -
1.58 - self.code = None
1.59 - self.code_location = None
1.60 -
1.61 # A record of nodes for which no attribute target could be found.
1.62
1.63 self.unknown_target_nodes = []
1.64 @@ -116,166 +98,6 @@
1.65
1.66 self.importer.finalise(objtable)
1.67
1.68 - def get_image(self, with_builtins=0):
1.69 -
1.70 - """
1.71 - Return the program image including built-in objects if 'with_builtins'
1.72 - is specified and set to a true value.
1.73 - """
1.74 -
1.75 - if self.code is not None:
1.76 - return self.code
1.77 -
1.78 - # Optimise and regenerate the object table.
1.79 -
1.80 - self.finalise()
1.81 - self.code = []
1.82 -
1.83 - # Append constants to the image.
1.84 -
1.85 - for const in self.importer.constants():
1.86 - self.code.append(const)
1.87 -
1.88 - # Generate each module.
1.89 -
1.90 - last_module = self.importer.modules_ordered[-1]
1.91 -
1.92 - for module in self.importer.modules_ordered:
1.93 - suppress_builtins = not with_builtins and module.name in ("__builtins__", "native")
1.94 -
1.95 - # Position the module in the image and make a translation.
1.96 -
1.97 - trans = micropython.ast.Translation(module, self)
1.98 -
1.99 - # Add header details.
1.100 -
1.101 - self.code.append(module)
1.102 -
1.103 - # Append module attributes to the image.
1.104 -
1.105 - attributes = module.module_attributes()
1.106 - self.code += module.attributes_as_list()
1.107 -
1.108 - # Append classes and functions to the image.
1.109 -
1.110 - for obj in module.all_objects:
1.111 - if isinstance(obj, Class):
1.112 -
1.113 - # Add header details.
1.114 -
1.115 - self.code.append(obj)
1.116 -
1.117 - # Append class attributes to the image.
1.118 -
1.119 - attributes = obj.class_attributes()
1.120 - self.code += obj.attributes_as_list()
1.121 -
1.122 - # Omit built-in function code where requested.
1.123 -
1.124 - if suppress_builtins and obj.astnode.doc is None:
1.125 - continue
1.126 -
1.127 - # Generate the instantiator/initialiser.
1.128 - # Append the function code to the image.
1.129 -
1.130 - code = trans.get_instantiator_code(obj)
1.131 - self.code += code
1.132 -
1.133 - # Class-level code is generated separately at the module
1.134 - # level, and the code location is set within the code
1.135 - # generation process for the module.
1.136 -
1.137 - elif isinstance(obj, Function):
1.138 -
1.139 - # Add header details.
1.140 -
1.141 - self.code.append(obj)
1.142 -
1.143 - # Append any default values to the image.
1.144 - # Only do this for functions which are not dynamic.
1.145 -
1.146 - if not obj.is_dynamic():
1.147 - self.code += obj.default_attrs
1.148 -
1.149 - # Omit built-in function code where requested.
1.150 -
1.151 - if suppress_builtins and obj.astnode.doc is None:
1.152 - pass
1.153 -
1.154 - # Append the function code to the image.
1.155 -
1.156 - else:
1.157 - code = trans.get_code(obj)
1.158 - self.code += code
1.159 -
1.160 - # Omit built-in module code where requested.
1.161 -
1.162 - if suppress_builtins:
1.163 - pass
1.164 -
1.165 - # Append the module top-level code to the image.
1.166 -
1.167 - else:
1.168 - code = trans.get_module_code()
1.169 - self.code += code
1.170 -
1.171 - # Generate the native library once we know how much of it is used.
1.172 -
1.173 - self.code += self.native.get_native_code()
1.174 -
1.175 - return self.code
1.176 -
1.177 - def get_raw_image(self, architecture=None, with_builtins=0):
1.178 -
1.179 - "Return the raw image representation of the program."
1.180 -
1.181 - architecture = architecture or micropython.rsvp
1.182 -
1.183 - self.get_image(with_builtins)
1.184 -
1.185 - objtable = self.get_object_table()
1.186 - paramtable = self.get_parameter_table()
1.187 -
1.188 - # Position the objects.
1.189 -
1.190 - pos = 0
1.191 -
1.192 - for item in self.code:
1.193 - arch_item = architecture.get_object(item)
1.194 -
1.195 - # Get the raw version for the architecture.
1.196 -
1.197 - if arch_item is not None:
1.198 - pos = arch_item.set_location(pos, objtable, with_builtins)
1.199 - else:
1.200 - pos += 1
1.201 -
1.202 - # Generate the raw code.
1.203 -
1.204 - self.raw_code = []
1.205 -
1.206 - for item in self.code:
1.207 - arch_item = architecture.get_object(item)
1.208 -
1.209 - # Get the raw version for the architecture.
1.210 -
1.211 - if arch_item is not None:
1.212 - arch_item.finalise_location(with_builtins)
1.213 - self.raw_code += arch_item.as_raw(objtable, paramtable, with_builtins)
1.214 - arch_item.finalise_body_location(with_builtins)
1.215 - else:
1.216 - self.raw_code.append(item)
1.217 -
1.218 - # Fix the module locations.
1.219 -
1.220 - for module in self.importer.modules_ordered:
1.221 -
1.222 - if not with_builtins and module.name in ("__builtins__", "native"):
1.223 - continue
1.224 -
1.225 - self.code_location = self.importer.modules["__main__"].code_location
1.226 - return self.raw_code
1.227 -
1.228 def get_object_table(self, reset=0):
1.229
1.230 "Return a table with details of attributes for classes and modules."
1.231 @@ -380,16 +202,6 @@
1.232
1.233 return self.paramtable
1.234
1.235 - def object_at(self, pos):
1.236 -
1.237 - "Return the object whose code can be found at 'pos'."
1.238 -
1.239 - i = bisect.bisect_left(self.code, Location(pos))
1.240 - if i > 0:
1.241 - return self.code[i-1]
1.242 - else:
1.243 - return None
1.244 -
1.245 class Importer:
1.246
1.247 "An import machine, searching for and loading modules."
1.248 @@ -406,7 +218,7 @@
1.249 "__call__"
1.250 ]
1.251
1.252 - def __init__(self, path=None, verbose=0, optimisations=None):
1.253 + def __init__(self, path=None, verbose=0):
1.254
1.255 """
1.256 Initialise the importer with the given search 'path' - a list of
1.257 @@ -414,14 +226,10 @@
1.258
1.259 The optional 'verbose' parameter causes output concerning the activities
1.260 of the object to be produced if set to a true value (not the default).
1.261 -
1.262 - The optional 'optimisations' cause certain techniques to be used in
1.263 - reducing program size and improving program efficiency.
1.264 """
1.265
1.266 self.path = path or [os.getcwd()]
1.267 self.verbose = verbose
1.268 - self.optimisations = optimisations or set()
1.269
1.270 self.modules = {}
1.271 self.modules_ordered = []