paul@92 | 1 | #!/usr/bin/env python |
paul@92 | 2 | |
paul@92 | 3 | """ |
paul@95 | 4 | Optimise object layouts and generate access instruction plans. |
paul@92 | 5 | |
paul@500 | 6 | Copyright (C) 2014, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@92 | 7 | |
paul@92 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@92 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@92 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@92 | 11 | version. |
paul@92 | 12 | |
paul@92 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@92 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@92 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@92 | 16 | details. |
paul@92 | 17 | |
paul@92 | 18 | You should have received a copy of the GNU General Public License along with |
paul@92 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@92 | 20 | """ |
paul@92 | 21 | |
paul@92 | 22 | from common import add_counter_item, get_attrname_from_location, init_item, \ |
paul@643 | 23 | sorted_output, CommonOutput |
paul@609 | 24 | from encoders import digest, encode_access_location, encode_instruction, get_kinds |
paul@620 | 25 | from errors import OptimiseError |
paul@92 | 26 | from os.path import exists, join |
paul@92 | 27 | from os import makedirs |
paul@643 | 28 | from referencing import decode_reference, Reference |
paul@92 | 29 | |
paul@643 | 30 | class Optimiser(CommonOutput): |
paul@92 | 31 | |
paul@92 | 32 | "Optimise objects in a program." |
paul@92 | 33 | |
paul@651 | 34 | def __init__(self, importer, deducer, output, |
paul@651 | 35 | attrnames_filename=None, locations_filename=None, |
paul@651 | 36 | paramnames_filename=None, parameter_locations_filename=None): |
paul@92 | 37 | |
paul@92 | 38 | """ |
paul@92 | 39 | Initialise an instance using the given 'importer' and 'deducer' that |
paul@92 | 40 | will perform the arrangement of attributes for program objects, writing |
paul@92 | 41 | the results to the given 'output' directory. |
paul@651 | 42 | |
paul@651 | 43 | If 'attrnames_filename', 'locations_filename', 'paramnames_filename', or |
paul@651 | 44 | 'parameter_locations_filename' are given, they will be used to |
paul@651 | 45 | explicitly indicate existing attribute code, attribute position, |
paul@651 | 46 | parameter code, and parameter position information respectively. |
paul@92 | 47 | """ |
paul@92 | 48 | |
paul@92 | 49 | self.importer = importer |
paul@92 | 50 | self.deducer = deducer |
paul@92 | 51 | self.output = output |
paul@92 | 52 | |
paul@651 | 53 | # Explicitly-specified attribute and parameter sources. |
paul@651 | 54 | |
paul@651 | 55 | self.attrnames_filename = attrnames_filename |
paul@651 | 56 | self.locations_filename = locations_filename |
paul@651 | 57 | self.paramnames_filename = paramnames_filename |
paul@651 | 58 | self.parameter_locations_filename = parameter_locations_filename |
paul@651 | 59 | |
paul@643 | 60 | # Detection of differences between any existing structure or signature |
paul@643 | 61 | # information and the generated information. |
paul@643 | 62 | |
paul@643 | 63 | self.differing_structures = [] |
paul@643 | 64 | self.differing_parameters = [] |
paul@643 | 65 | |
paul@92 | 66 | # Locations/offsets of attributes in objects. |
paul@92 | 67 | |
paul@92 | 68 | self.locations = None |
paul@643 | 69 | self.existing_locations = None |
paul@643 | 70 | |
paul@92 | 71 | self.attr_locations = None |
paul@92 | 72 | |
paul@643 | 73 | # Attribute code assignments. |
paul@643 | 74 | |
paul@643 | 75 | self.all_attrnames = None |
paul@643 | 76 | self.existing_attrnames = None |
paul@651 | 77 | self.indicated_attrnames = None |
paul@641 | 78 | |
paul@92 | 79 | # Locations of parameters in parameter tables. |
paul@92 | 80 | |
paul@92 | 81 | self.arg_locations = None |
paul@643 | 82 | self.existing_arg_locations = None |
paul@643 | 83 | |
paul@92 | 84 | self.param_locations = None |
paul@92 | 85 | |
paul@643 | 86 | # Parameter code assignments. |
paul@643 | 87 | |
paul@643 | 88 | self.all_paramnames = None |
paul@643 | 89 | self.existing_paramnames = None |
paul@651 | 90 | self.indicated_paramnames = None |
paul@641 | 91 | |
paul@92 | 92 | # Specific attribute access information. |
paul@92 | 93 | |
paul@94 | 94 | self.access_instructions = {} |
paul@234 | 95 | self.accessor_kinds = {} |
paul@92 | 96 | |
paul@92 | 97 | # Object structure information. |
paul@92 | 98 | |
paul@92 | 99 | self.structures = {} |
paul@643 | 100 | self.existing_structures = None |
paul@92 | 101 | self.attr_table = {} |
paul@92 | 102 | |
paul@92 | 103 | # Parameter list information. |
paul@92 | 104 | |
paul@92 | 105 | self.parameters = {} |
paul@643 | 106 | self.existing_parameters = None |
paul@92 | 107 | self.param_table = {} |
paul@92 | 108 | |
paul@92 | 109 | # Constant literal information. |
paul@92 | 110 | |
paul@92 | 111 | self.constants = [] |
paul@92 | 112 | self.constant_numbers = {} |
paul@620 | 113 | self.digests = {} |
paul@92 | 114 | |
paul@92 | 115 | # Optimiser activities. |
paul@92 | 116 | |
paul@641 | 117 | self.from_output() |
paul@641 | 118 | |
paul@641 | 119 | # Define or redefine structure information. |
paul@641 | 120 | |
paul@92 | 121 | self.populate_objects() |
paul@92 | 122 | self.position_attributes() |
paul@92 | 123 | self.populate_parameters() |
paul@92 | 124 | self.position_parameters() |
paul@92 | 125 | self.populate_tables() |
paul@92 | 126 | self.populate_constants() |
paul@94 | 127 | self.initialise_access_instructions() |
paul@92 | 128 | |
paul@643 | 129 | def need_reset(self): |
paul@643 | 130 | |
paul@643 | 131 | """ |
paul@643 | 132 | Return whether attribute or parameter information has changed, requiring |
paul@643 | 133 | the reset/recompilation of all source files. |
paul@643 | 134 | """ |
paul@643 | 135 | |
paul@643 | 136 | return self.differing_structures or self.differing_parameters |
paul@643 | 137 | |
paul@641 | 138 | def from_output(self): |
paul@641 | 139 | |
paul@641 | 140 | "Read input files that influence optimisation." |
paul@641 | 141 | |
paul@643 | 142 | # Remove any output for a different program. |
paul@643 | 143 | |
paul@643 | 144 | self.check_output() |
paul@643 | 145 | |
paul@651 | 146 | # Existing attribute and parameter positioning information. This |
paul@651 | 147 | # influences the positions of attributes and parameters found in the |
paul@651 | 148 | # program. |
paul@651 | 149 | |
paul@651 | 150 | locations_filename = self.locations_filename or \ |
paul@651 | 151 | join(self.output, "locations") |
paul@651 | 152 | |
paul@651 | 153 | parameter_locations_filename = self.parameter_locations_filename or \ |
paul@651 | 154 | join(self.output, "parameter_locations") |
paul@643 | 155 | |
paul@651 | 156 | self.existing_locations = self.read_data(locations_filename, self._line_to_list, list) |
paul@651 | 157 | self.existing_arg_locations = self.read_data(parameter_locations_filename, self._line_to_list, list) |
paul@643 | 158 | |
paul@651 | 159 | # Existing attribute and parameter code information. This is used to |
paul@651 | 160 | # check the compatibility of the output against any assignments |
paul@651 | 161 | # previously made. |
paul@651 | 162 | |
paul@651 | 163 | identity = lambda x: x |
paul@651 | 164 | none = lambda x: None |
paul@643 | 165 | |
paul@651 | 166 | attrnames_filename = join(self.output, "attrnames") |
paul@651 | 167 | paramnames_filename = join(self.output, "paramnames") |
paul@651 | 168 | |
paul@651 | 169 | self.existing_attrnames = self.read_data(attrnames_filename, identity, none) |
paul@651 | 170 | self.existing_paramnames = self.read_data(paramnames_filename, identity, none) |
paul@651 | 171 | |
paul@651 | 172 | # Explicitly-specified attribute name and parameter name codes. These |
paul@651 | 173 | # direct assignment of codes in the program. |
paul@651 | 174 | |
paul@651 | 175 | self.indicated_attrnames = self.attrnames_filename and \ |
paul@651 | 176 | self.read_data(self.attrnames_filename, identity, none) |
paul@643 | 177 | |
paul@651 | 178 | self.indicated_paramnames = self.paramnames_filename and \ |
paul@651 | 179 | self.read_data(self.paramnames_filename, identity, none) |
paul@651 | 180 | |
paul@651 | 181 | # Existing structure and signature information. This is used to check |
paul@651 | 182 | # the output and detect whether structures or signatures have changed. |
paul@643 | 183 | |
paul@651 | 184 | structures_filename = join(self.output, "structures") |
paul@651 | 185 | parameters_filename = join(self.output, "parameters") |
paul@651 | 186 | |
paul@651 | 187 | self.existing_structures = dict(self.read_data(structures_filename, self._line_to_structure_pairs, list)) |
paul@651 | 188 | self.existing_parameters = dict(self.read_data(parameters_filename, self._line_to_signature_pairs, list)) |
paul@641 | 189 | |
paul@641 | 190 | def _line_to_list(self, line): |
paul@641 | 191 | |
paul@641 | 192 | "Convert comma-separated values in 'line' to a list of values." |
paul@641 | 193 | |
paul@641 | 194 | return line.split(", ") |
paul@641 | 195 | |
paul@643 | 196 | def _line_to_signature_pairs(self, line): |
paul@643 | 197 | |
paul@643 | 198 | "Convert comma-separated values in 'line' to a list of pairs of values." |
paul@643 | 199 | |
paul@643 | 200 | l = [] |
paul@643 | 201 | objpath, line = line.split(" ", 1) |
paul@643 | 202 | for values in line.split(", "): |
paul@643 | 203 | if values != "-": |
paul@643 | 204 | name, pos = values.split(":") |
paul@643 | 205 | l.append((name, int(pos))) |
paul@643 | 206 | else: |
paul@643 | 207 | l.append(None) |
paul@643 | 208 | return (objpath, l) |
paul@643 | 209 | |
paul@643 | 210 | def _line_to_structure_pairs(self, line): |
paul@643 | 211 | |
paul@643 | 212 | "Convert comma-separated values in 'line' to a list of pairs of values." |
paul@643 | 213 | |
paul@643 | 214 | l = [] |
paul@643 | 215 | ref, line = line.split(" ", 1) |
paul@643 | 216 | values = map(lambda x: x != '-' and x or None, line.split(", ")) |
paul@643 | 217 | return (decode_reference(ref), values) |
paul@643 | 218 | |
paul@651 | 219 | def read_data(self, filename, decode, empty): |
paul@641 | 220 | |
paul@641 | 221 | """ |
paul@643 | 222 | Read location details from 'filename', using 'decode' to convert each |
paul@643 | 223 | line and 'empty' to produce an empty result where no data is given on a |
paul@643 | 224 | line, returning a collection. |
paul@641 | 225 | """ |
paul@641 | 226 | |
paul@643 | 227 | collection = [] |
paul@641 | 228 | |
paul@641 | 229 | if exists(filename): |
paul@641 | 230 | f = open(filename) |
paul@641 | 231 | try: |
paul@641 | 232 | for line in f.readlines(): |
paul@641 | 233 | line = line.rstrip() |
paul@641 | 234 | if line: |
paul@641 | 235 | attrnames = decode(line) |
paul@641 | 236 | else: |
paul@641 | 237 | attrnames = empty() |
paul@641 | 238 | |
paul@641 | 239 | collection.append(attrnames) |
paul@641 | 240 | finally: |
paul@641 | 241 | f.close() |
paul@641 | 242 | |
paul@643 | 243 | return collection |
paul@643 | 244 | |
paul@92 | 245 | def to_output(self): |
paul@92 | 246 | |
paul@92 | 247 | "Write the output files using optimisation information." |
paul@92 | 248 | |
paul@92 | 249 | if not exists(self.output): |
paul@92 | 250 | makedirs(self.output) |
paul@92 | 251 | |
paul@92 | 252 | self.write_objects() |
paul@92 | 253 | |
paul@92 | 254 | def write_objects(self): |
paul@92 | 255 | |
paul@92 | 256 | """ |
paul@92 | 257 | Write object-related output. |
paul@92 | 258 | |
paul@92 | 259 | The locations are a list of positions indicating the attributes residing |
paul@92 | 260 | at each position in the different structures in a program. |
paul@92 | 261 | |
paul@92 | 262 | ---- |
paul@92 | 263 | |
paul@92 | 264 | The parameter locations are a list of positions indicating the parameters |
paul@92 | 265 | residing at each position in the different parameter lists in a program. |
paul@92 | 266 | |
paul@92 | 267 | ---- |
paul@92 | 268 | |
paul@92 | 269 | Each attribute plan provides attribute details in the following format: |
paul@92 | 270 | |
paul@92 | 271 | location " " name " " test " " test type " " base |
paul@92 | 272 | " " traversed attributes " " traversed attribute ambiguity |
paul@96 | 273 | " " traversal access modes |
paul@92 | 274 | " " attributes to traverse " " attribute ambiguity |
paul@92 | 275 | " " context " " access method " " static attribute |
paul@92 | 276 | |
paul@92 | 277 | Locations have the following format: |
paul@92 | 278 | |
paul@92 | 279 | qualified name of scope "." local name ":" name version |
paul@92 | 280 | |
paul@96 | 281 | Traversal access modes are either "class" (obtain accessor class to |
paul@96 | 282 | access attribute) or "object" (obtain attribute directly from accessor). |
paul@96 | 283 | |
paul@92 | 284 | ---- |
paul@92 | 285 | |
paul@92 | 286 | The structures are presented as a table in the following format: |
paul@92 | 287 | |
paul@92 | 288 | qualified name " " attribute names |
paul@92 | 289 | |
paul@92 | 290 | The attribute names are separated by ", " characters and indicate the |
paul@92 | 291 | attribute provided at each position in the structure associated with the |
paul@92 | 292 | given type. Where no attribute is provided at a particular location |
paul@92 | 293 | within a structure, "-" is given. |
paul@92 | 294 | |
paul@92 | 295 | ---- |
paul@92 | 296 | |
paul@92 | 297 | The parameters are presented as a table in the following format: |
paul@92 | 298 | |
paul@92 | 299 | qualified name " " parameter details |
paul@92 | 300 | |
paul@92 | 301 | The parameter details are separated by ", " characters and indicate |
paul@92 | 302 | the parameter name and list position for each parameter described at |
paul@92 | 303 | each location in the parameter table associated with the given |
paul@92 | 304 | function. Where no parameter details are provided at a particular |
paul@92 | 305 | location within a parameter table, "-" is given. The name and list |
paul@92 | 306 | position are separated by a colon (":"). |
paul@92 | 307 | |
paul@92 | 308 | ---- |
paul@92 | 309 | |
paul@92 | 310 | The attribute table is presented as a table in the following format: |
paul@92 | 311 | |
paul@92 | 312 | qualified name " " attribute identifiers |
paul@92 | 313 | |
paul@92 | 314 | Instead of attribute names, identifiers defined according to the order |
paul@92 | 315 | given in the "attrnames" file are employed to denote the attributes |
paul@92 | 316 | featured in each type's structure. Where no attribute is provided at a |
paul@92 | 317 | particular location within a structure, "-" is given. |
paul@92 | 318 | |
paul@92 | 319 | ---- |
paul@92 | 320 | |
paul@92 | 321 | The parameter table is presented as a table in the following format: |
paul@92 | 322 | |
paul@92 | 323 | qualified name " " parameter details |
paul@92 | 324 | |
paul@92 | 325 | Instead of parameter names, identifiers defined according to the order |
paul@92 | 326 | given in the "paramnames" file are employed to denote the parameters |
paul@92 | 327 | featured in each function's parameter table. Where no parameter is |
paul@92 | 328 | provided at a particular location within a table, "-" is given. |
paul@92 | 329 | |
paul@92 | 330 | ---- |
paul@92 | 331 | |
paul@92 | 332 | The ordered list of attribute names is given in the "attrnames" file. |
paul@92 | 333 | |
paul@92 | 334 | ---- |
paul@92 | 335 | |
paul@92 | 336 | The ordered list of parameter names is given in the "paramnames" file. |
paul@92 | 337 | |
paul@92 | 338 | ---- |
paul@92 | 339 | |
paul@92 | 340 | The ordered list of constant literals is given in the "constants" file. |
paul@92 | 341 | """ |
paul@92 | 342 | |
paul@92 | 343 | f = open(join(self.output, "locations"), "w") |
paul@92 | 344 | try: |
paul@92 | 345 | for attrnames in self.locations: |
paul@92 | 346 | print >>f, sorted_output(attrnames) |
paul@92 | 347 | |
paul@92 | 348 | finally: |
paul@92 | 349 | f.close() |
paul@92 | 350 | |
paul@92 | 351 | f = open(join(self.output, "parameter_locations"), "w") |
paul@92 | 352 | try: |
paul@92 | 353 | for argnames in self.arg_locations: |
paul@92 | 354 | print >>f, sorted_output(argnames) |
paul@92 | 355 | |
paul@92 | 356 | finally: |
paul@92 | 357 | f.close() |
paul@92 | 358 | |
paul@94 | 359 | f = open(join(self.output, "instruction_plans"), "w") |
paul@94 | 360 | try: |
paul@94 | 361 | access_instructions = self.access_instructions.items() |
paul@94 | 362 | access_instructions.sort() |
paul@94 | 363 | |
paul@94 | 364 | for location, instructions in access_instructions: |
paul@94 | 365 | print >>f, encode_access_location(location), "..." |
paul@94 | 366 | for instruction in instructions: |
paul@94 | 367 | print >>f, encode_instruction(instruction) |
paul@94 | 368 | print >>f |
paul@92 | 369 | |
paul@92 | 370 | finally: |
paul@92 | 371 | f.close() |
paul@92 | 372 | |
paul@92 | 373 | f = open(join(self.output, "structures"), "w") |
paul@92 | 374 | try: |
paul@92 | 375 | structures = self.structures.items() |
paul@92 | 376 | structures.sort() |
paul@92 | 377 | |
paul@92 | 378 | for name, attrnames in structures: |
paul@92 | 379 | print >>f, name, ", ".join([s or "-" for s in attrnames]) |
paul@92 | 380 | |
paul@92 | 381 | finally: |
paul@92 | 382 | f.close() |
paul@92 | 383 | |
paul@92 | 384 | f = open(join(self.output, "parameters"), "w") |
paul@92 | 385 | try: |
paul@92 | 386 | parameters = self.parameters.items() |
paul@92 | 387 | parameters.sort() |
paul@92 | 388 | |
paul@92 | 389 | for name, argnames in parameters: |
paul@642 | 390 | l = [] |
paul@642 | 391 | for s in argnames: |
paul@642 | 392 | l.append(s and ("%s:%d" % s) or "-") |
paul@642 | 393 | print >>f, name, ", ".join(l) |
paul@92 | 394 | |
paul@92 | 395 | finally: |
paul@92 | 396 | f.close() |
paul@92 | 397 | |
paul@92 | 398 | f = open(join(self.output, "attrtable"), "w") |
paul@92 | 399 | try: |
paul@92 | 400 | attr_table = self.attr_table.items() |
paul@92 | 401 | attr_table.sort() |
paul@92 | 402 | |
paul@92 | 403 | for name, attrcodes in attr_table: |
paul@642 | 404 | l = [] |
paul@642 | 405 | for i in attrcodes: |
paul@642 | 406 | l.append(i is not None and str(i) or "-") |
paul@642 | 407 | print >>f, name, ", ".join(l) |
paul@92 | 408 | |
paul@92 | 409 | finally: |
paul@92 | 410 | f.close() |
paul@92 | 411 | |
paul@92 | 412 | f = open(join(self.output, "paramtable"), "w") |
paul@92 | 413 | try: |
paul@92 | 414 | param_table = self.param_table.items() |
paul@92 | 415 | param_table.sort() |
paul@92 | 416 | |
paul@92 | 417 | for name, paramcodes in param_table: |
paul@642 | 418 | l = [] |
paul@642 | 419 | for s in paramcodes: |
paul@642 | 420 | l.append(s and ("%d:%d" % s) or "-") |
paul@642 | 421 | print >>f, name, ", ".join(l) |
paul@92 | 422 | |
paul@92 | 423 | finally: |
paul@92 | 424 | f.close() |
paul@92 | 425 | |
paul@92 | 426 | f = open(join(self.output, "attrnames"), "w") |
paul@92 | 427 | try: |
paul@92 | 428 | for name in self.all_attrnames: |
paul@92 | 429 | print >>f, name |
paul@92 | 430 | |
paul@92 | 431 | finally: |
paul@92 | 432 | f.close() |
paul@92 | 433 | |
paul@92 | 434 | f = open(join(self.output, "paramnames"), "w") |
paul@92 | 435 | try: |
paul@92 | 436 | for name in self.all_paramnames: |
paul@92 | 437 | print >>f, name |
paul@92 | 438 | |
paul@92 | 439 | finally: |
paul@92 | 440 | f.close() |
paul@92 | 441 | |
paul@92 | 442 | f = open(join(self.output, "constants"), "w") |
paul@92 | 443 | try: |
paul@397 | 444 | constants = [] |
paul@406 | 445 | for (value, value_type, encoding), n in self.constants.items(): |
paul@406 | 446 | constants.append((n, value_type, encoding, value)) |
paul@92 | 447 | constants.sort() |
paul@406 | 448 | for n, value_type, encoding, value in constants: |
paul@406 | 449 | print >>f, value_type, encoding or "{}", repr(value) |
paul@92 | 450 | |
paul@92 | 451 | finally: |
paul@92 | 452 | f.close() |
paul@92 | 453 | |
paul@92 | 454 | def populate_objects(self): |
paul@92 | 455 | |
paul@92 | 456 | "Populate objects using attribute and usage information." |
paul@92 | 457 | |
paul@559 | 458 | self.all_attrs = {} |
paul@92 | 459 | |
paul@92 | 460 | # Partition attributes into separate sections so that class and instance |
paul@92 | 461 | # attributes are treated separately. |
paul@92 | 462 | |
paul@564 | 463 | for source, objkind in [ |
paul@92 | 464 | (self.importer.all_class_attrs, "<class>"), |
paul@92 | 465 | (self.importer.all_instance_attrs, "<instance>"), |
paul@92 | 466 | (self.importer.all_module_attrs, "<module>") |
paul@92 | 467 | ]: |
paul@92 | 468 | |
paul@559 | 469 | for name, attrnames in source.items(): |
paul@561 | 470 | |
paul@561 | 471 | # Remove temporary names from structures. |
paul@561 | 472 | |
paul@561 | 473 | attrnames = filter(lambda x: not x.startswith("$t"), attrnames) |
paul@564 | 474 | self.all_attrs[(objkind, name)] = attrnames |
paul@559 | 475 | |
paul@640 | 476 | self.locations = get_allocated_locations(self.all_attrs, |
paul@641 | 477 | get_attributes_and_sizes, self.existing_locations) |
paul@92 | 478 | |
paul@92 | 479 | def populate_parameters(self): |
paul@92 | 480 | |
paul@92 | 481 | "Populate parameter tables using parameter information." |
paul@92 | 482 | |
paul@641 | 483 | # Allocate positions from 1 onwards, ignoring the context argument. |
paul@641 | 484 | |
paul@640 | 485 | self.arg_locations = [set()] + get_allocated_locations( |
paul@641 | 486 | self.importer.function_parameters, get_parameters_and_sizes, |
paul@641 | 487 | self.existing_arg_locations[1:]) |
paul@92 | 488 | |
paul@92 | 489 | def position_attributes(self): |
paul@92 | 490 | |
paul@92 | 491 | "Position specific attribute references." |
paul@92 | 492 | |
paul@641 | 493 | # Reverse the location mappings, producing a mapping from attribute |
paul@641 | 494 | # names to positions. |
paul@92 | 495 | |
paul@92 | 496 | attr_locations = self.attr_locations = {} |
paul@641 | 497 | self._position_attributes(attr_locations, self.locations) |
paul@92 | 498 | |
paul@641 | 499 | # Add any previously-known attribute locations. This prevents attributes |
paul@641 | 500 | # from being assigned different identifying codes by preserving obsolete |
paul@641 | 501 | # attribute codes. |
paul@641 | 502 | |
paul@641 | 503 | if self.existing_locations: |
paul@641 | 504 | self._position_attributes(attr_locations, self.existing_locations) |
paul@92 | 505 | |
paul@92 | 506 | # Record the structures. |
paul@92 | 507 | |
paul@564 | 508 | for (objkind, name), attrnames in self.all_attrs.items(): |
paul@564 | 509 | key = Reference(objkind, name) |
paul@559 | 510 | l = self.structures[key] = [None] * len(attrnames) |
paul@643 | 511 | |
paul@559 | 512 | for attrname in attrnames: |
paul@559 | 513 | position = attr_locations[attrname] |
paul@559 | 514 | if position >= len(l): |
paul@559 | 515 | l.extend([None] * (position - len(l) + 1)) |
paul@559 | 516 | l[position] = attrname |
paul@92 | 517 | |
paul@643 | 518 | # Test the structure against any existing attributes. |
paul@643 | 519 | |
paul@643 | 520 | if self.existing_structures: |
paul@643 | 521 | if self.existing_structures.has_key(key) and self.existing_structures[key] != l: |
paul@643 | 522 | self.differing_structures.append(key) |
paul@643 | 523 | |
paul@641 | 524 | def _position_attributes(self, d, l): |
paul@641 | 525 | |
paul@641 | 526 | """ |
paul@641 | 527 | For each attribute, store a mapping in 'd' to the index in 'l' at which |
paul@641 | 528 | it can be found. |
paul@641 | 529 | """ |
paul@641 | 530 | |
paul@641 | 531 | for i, attrnames in enumerate(l): |
paul@641 | 532 | for attrname in attrnames: |
paul@641 | 533 | if not d.has_key(attrname): |
paul@641 | 534 | d[attrname] = i |
paul@641 | 535 | |
paul@94 | 536 | def initialise_access_instructions(self): |
paul@94 | 537 | |
paul@94 | 538 | "Expand access plans into instruction sequences." |
paul@94 | 539 | |
paul@97 | 540 | for access_location, access_plan in self.deducer.access_plans.items(): |
paul@94 | 541 | |
paul@94 | 542 | # Obtain the access details. |
paul@94 | 543 | |
paul@234 | 544 | name, test, test_type, base, \ |
paul@234 | 545 | traversed, traversal_modes, attrnames, \ |
paul@234 | 546 | context, context_test, \ |
paul@234 | 547 | first_method, final_method, \ |
paul@234 | 548 | origin, accessor_kinds = access_plan |
paul@94 | 549 | |
paul@596 | 550 | # Emit instructions by appending them to a list. |
paul@596 | 551 | |
paul@94 | 552 | instructions = [] |
paul@94 | 553 | emit = instructions.append |
paul@94 | 554 | |
paul@596 | 555 | # Identify any static original accessor. |
paul@596 | 556 | |
paul@94 | 557 | if base: |
paul@94 | 558 | original_accessor = base |
paul@618 | 559 | |
paul@618 | 560 | # Employ names as contexts unless the context needs testing and |
paul@618 | 561 | # potentially updating. In such cases, temporary context storage is |
paul@618 | 562 | # used instead. |
paul@618 | 563 | |
paul@618 | 564 | elif name and not (context_test == "test" and |
paul@618 | 565 | final_method in ("access-invoke", "static-invoke")): |
paul@618 | 566 | original_accessor = "<name>" # refers to the name |
paul@618 | 567 | |
paul@618 | 568 | # Use a generic placeholder representing the access expression in |
paul@618 | 569 | # the general case. |
paul@618 | 570 | |
paul@94 | 571 | else: |
paul@618 | 572 | original_accessor = "<expr>" |
paul@94 | 573 | |
paul@94 | 574 | # Prepare for any first attribute access. |
paul@94 | 575 | |
paul@94 | 576 | if traversed: |
paul@94 | 577 | attrname = traversed[0] |
paul@94 | 578 | del traversed[0] |
paul@94 | 579 | elif attrnames: |
paul@94 | 580 | attrname = attrnames[0] |
paul@94 | 581 | del attrnames[0] |
paul@94 | 582 | |
paul@98 | 583 | # Perform the first access explicitly if at least one operation |
paul@98 | 584 | # requires it. |
paul@98 | 585 | |
paul@587 | 586 | access_first_attribute = final_method in ("access", "access-invoke", "assign") or traversed or attrnames |
paul@98 | 587 | |
paul@98 | 588 | # Determine whether the first access involves assignment. |
paul@98 | 589 | |
paul@98 | 590 | assigning = not traversed and not attrnames and final_method == "assign" |
paul@482 | 591 | set_accessor = assigning and "<set_target_accessor>" or "<set_accessor>" |
paul@368 | 592 | stored_accessor = assigning and "<target_accessor>" or "<accessor>" |
paul@94 | 593 | |
paul@94 | 594 | # Set the context if already available. |
paul@100 | 595 | |
paul@618 | 596 | context_var = None |
paul@618 | 597 | |
paul@103 | 598 | if context == "base": |
paul@103 | 599 | accessor = context_var = (base,) |
paul@103 | 600 | elif context == "original-accessor": |
paul@104 | 601 | |
paul@104 | 602 | # Prevent re-evaluation of any dynamic expression by storing it. |
paul@104 | 603 | |
paul@103 | 604 | if original_accessor == "<expr>": |
paul@587 | 605 | if final_method in ("access-invoke", "static-invoke"): |
paul@587 | 606 | emit(("<set_context>", original_accessor)) |
paul@587 | 607 | accessor = context_var = ("<context>",) |
paul@587 | 608 | else: |
paul@587 | 609 | emit((set_accessor, original_accessor)) |
paul@587 | 610 | accessor = context_var = (stored_accessor,) |
paul@103 | 611 | else: |
paul@104 | 612 | accessor = context_var = (original_accessor,) |
paul@100 | 613 | |
paul@98 | 614 | # Assigning does not set the context. |
paul@94 | 615 | |
paul@102 | 616 | elif context in ("final-accessor", "unset") and access_first_attribute: |
paul@104 | 617 | |
paul@104 | 618 | # Prevent re-evaluation of any dynamic expression by storing it. |
paul@104 | 619 | |
paul@103 | 620 | if original_accessor == "<expr>": |
paul@368 | 621 | emit((set_accessor, original_accessor)) |
paul@368 | 622 | accessor = (stored_accessor,) |
paul@103 | 623 | else: |
paul@104 | 624 | accessor = (original_accessor,) |
paul@94 | 625 | |
paul@94 | 626 | # Apply any test. |
paul@94 | 627 | |
paul@236 | 628 | if test[0] == "test": |
paul@236 | 629 | accessor = ("__%s_%s_%s" % test, accessor, test_type) |
paul@94 | 630 | |
paul@94 | 631 | # Perform the first or final access. |
paul@94 | 632 | # The access only needs performing if the resulting accessor is used. |
paul@94 | 633 | |
paul@102 | 634 | remaining = len(traversed + attrnames) |
paul@102 | 635 | |
paul@94 | 636 | if access_first_attribute: |
paul@94 | 637 | |
paul@94 | 638 | if first_method == "relative-class": |
paul@98 | 639 | if assigning: |
paul@113 | 640 | emit(("__store_via_class", accessor, attrname, "<assexpr>")) |
paul@98 | 641 | else: |
paul@113 | 642 | accessor = ("__load_via_class", accessor, attrname) |
paul@98 | 643 | |
paul@94 | 644 | elif first_method == "relative-object": |
paul@98 | 645 | if assigning: |
paul@113 | 646 | emit(("__store_via_object", accessor, attrname, "<assexpr>")) |
paul@98 | 647 | else: |
paul@113 | 648 | accessor = ("__load_via_object", accessor, attrname) |
paul@98 | 649 | |
paul@94 | 650 | elif first_method == "relative-object-class": |
paul@98 | 651 | if assigning: |
paul@113 | 652 | emit(("__get_class_and_store", accessor, attrname, "<assexpr>")) |
paul@98 | 653 | else: |
paul@113 | 654 | accessor = ("__get_class_and_load", accessor, attrname) |
paul@98 | 655 | |
paul@94 | 656 | elif first_method == "check-class": |
paul@98 | 657 | if assigning: |
paul@113 | 658 | emit(("__check_and_store_via_class", accessor, attrname, "<assexpr>")) |
paul@98 | 659 | else: |
paul@113 | 660 | accessor = ("__check_and_load_via_class", accessor, attrname) |
paul@98 | 661 | |
paul@94 | 662 | elif first_method == "check-object": |
paul@98 | 663 | if assigning: |
paul@113 | 664 | emit(("__check_and_store_via_object", accessor, attrname, "<assexpr>")) |
paul@98 | 665 | else: |
paul@113 | 666 | accessor = ("__check_and_load_via_object", accessor, attrname) |
paul@98 | 667 | |
paul@94 | 668 | elif first_method == "check-object-class": |
paul@98 | 669 | if assigning: |
paul@113 | 670 | emit(("__check_and_store_via_any", accessor, attrname, "<assexpr>")) |
paul@98 | 671 | else: |
paul@113 | 672 | accessor = ("__check_and_load_via_any", accessor, attrname) |
paul@94 | 673 | |
paul@102 | 674 | # Traverse attributes using the accessor. |
paul@94 | 675 | |
paul@94 | 676 | if traversed: |
paul@96 | 677 | for attrname, traversal_mode in zip(traversed, traversal_modes): |
paul@98 | 678 | assigning = remaining == 1 and final_method == "assign" |
paul@94 | 679 | |
paul@94 | 680 | # Set the context, if appropriate. |
paul@94 | 681 | |
paul@98 | 682 | if remaining == 1 and final_method != "assign" and context == "final-accessor": |
paul@596 | 683 | |
paul@596 | 684 | # Invoked attributes employ a separate context accessed |
paul@596 | 685 | # during invocation. |
paul@596 | 686 | |
paul@592 | 687 | if final_method in ("access-invoke", "static-invoke"): |
paul@592 | 688 | emit(("<set_context>", accessor)) |
paul@592 | 689 | accessor = context_var = "<context>" |
paul@596 | 690 | |
paul@596 | 691 | # A private context within the access is otherwise |
paul@596 | 692 | # retained. |
paul@596 | 693 | |
paul@592 | 694 | else: |
paul@592 | 695 | emit(("<set_private_context>", accessor)) |
paul@592 | 696 | accessor = context_var = "<private_context>" |
paul@94 | 697 | |
paul@94 | 698 | # Perform the access only if not achieved directly. |
paul@94 | 699 | |
paul@587 | 700 | if remaining > 1 or final_method in ("access", "access-invoke", "assign"): |
paul@98 | 701 | |
paul@96 | 702 | if traversal_mode == "class": |
paul@98 | 703 | if assigning: |
paul@113 | 704 | emit(("__store_via_class", accessor, attrname, "<assexpr>")) |
paul@98 | 705 | else: |
paul@113 | 706 | accessor = ("__load_via_class", accessor, attrname) |
paul@96 | 707 | else: |
paul@98 | 708 | if assigning: |
paul@113 | 709 | emit(("__store_via_object", accessor, attrname, "<assexpr>")) |
paul@98 | 710 | else: |
paul@113 | 711 | accessor = ("__load_via_object", accessor, attrname) |
paul@94 | 712 | |
paul@94 | 713 | remaining -= 1 |
paul@94 | 714 | |
paul@94 | 715 | if attrnames: |
paul@96 | 716 | for attrname in attrnames: |
paul@98 | 717 | assigning = remaining == 1 and final_method == "assign" |
paul@94 | 718 | |
paul@94 | 719 | # Set the context, if appropriate. |
paul@94 | 720 | |
paul@98 | 721 | if remaining == 1 and final_method != "assign" and context == "final-accessor": |
paul@596 | 722 | |
paul@596 | 723 | # Invoked attributes employ a separate context accessed |
paul@596 | 724 | # during invocation. |
paul@596 | 725 | |
paul@592 | 726 | if final_method in ("access-invoke", "static-invoke"): |
paul@592 | 727 | emit(("<set_context>", accessor)) |
paul@592 | 728 | accessor = context_var = "<context>" |
paul@596 | 729 | |
paul@596 | 730 | # A private context within the access is otherwise |
paul@596 | 731 | # retained. |
paul@596 | 732 | |
paul@592 | 733 | else: |
paul@592 | 734 | emit(("<set_private_context>", accessor)) |
paul@592 | 735 | accessor = context_var = "<private_context>" |
paul@94 | 736 | |
paul@94 | 737 | # Perform the access only if not achieved directly. |
paul@94 | 738 | |
paul@587 | 739 | if remaining > 1 or final_method in ("access", "access-invoke", "assign"): |
paul@98 | 740 | |
paul@98 | 741 | if assigning: |
paul@113 | 742 | emit(("__check_and_store_via_any", accessor, attrname, "<assexpr>")) |
paul@98 | 743 | else: |
paul@113 | 744 | accessor = ("__check_and_load_via_any", accessor, attrname) |
paul@94 | 745 | |
paul@94 | 746 | remaining -= 1 |
paul@94 | 747 | |
paul@118 | 748 | # Define or emit the means of accessing the actual target. |
paul@118 | 749 | |
paul@587 | 750 | # Assignments to known attributes. |
paul@587 | 751 | |
paul@98 | 752 | if final_method == "static-assign": |
paul@118 | 753 | parent, attrname = origin.rsplit(".", 1) |
paul@118 | 754 | emit(("__store_via_object", parent, attrname, "<assexpr>")) |
paul@118 | 755 | |
paul@587 | 756 | # Invoked attributes employ a separate context. |
paul@587 | 757 | |
paul@200 | 758 | elif final_method in ("static", "static-invoke"): |
paul@577 | 759 | accessor = ("__load_static_ignore", origin) |
paul@118 | 760 | |
paul@118 | 761 | # Wrap accesses in context operations. |
paul@118 | 762 | |
paul@102 | 763 | if context_test == "test": |
paul@596 | 764 | |
paul@596 | 765 | # Test and combine the context with static attribute details. |
paul@596 | 766 | |
paul@595 | 767 | if final_method == "static": |
paul@577 | 768 | emit(("__load_static_test", context_var, origin)) |
paul@596 | 769 | |
paul@596 | 770 | # Test the context, storing it separately if required for the |
paul@596 | 771 | # immediately invoked static attribute. |
paul@596 | 772 | |
paul@595 | 773 | elif final_method == "static-invoke": |
paul@595 | 774 | emit(("<test_context_static>", context_var, origin)) |
paul@596 | 775 | |
paul@596 | 776 | # Test the context, storing it separately if required for an |
paul@596 | 777 | # immediately invoked attribute. |
paul@596 | 778 | |
paul@594 | 779 | elif final_method == "access-invoke": |
paul@601 | 780 | emit(("<test_context_revert>", context_var, accessor)) |
paul@596 | 781 | |
paul@596 | 782 | # Test the context and update the attribute details if |
paul@596 | 783 | # appropriate. |
paul@596 | 784 | |
paul@577 | 785 | else: |
paul@577 | 786 | emit(("__test_context", context_var, accessor)) |
paul@118 | 787 | |
paul@102 | 788 | elif context_test == "replace": |
paul@587 | 789 | |
paul@587 | 790 | # Produce an object with updated context. |
paul@587 | 791 | |
paul@587 | 792 | if final_method == "static": |
paul@577 | 793 | emit(("__load_static_replace", context_var, origin)) |
paul@587 | 794 | |
paul@588 | 795 | # Omit the context update operation where the target is static |
paul@588 | 796 | # and the context is recorded separately. |
paul@588 | 797 | |
paul@588 | 798 | elif final_method == "static-invoke": |
paul@588 | 799 | pass |
paul@588 | 800 | |
paul@596 | 801 | # If a separate context is used for an immediate invocation, |
paul@596 | 802 | # produce the attribute details unchanged. |
paul@587 | 803 | |
paul@596 | 804 | elif final_method == "access-invoke": |
paul@596 | 805 | emit(accessor) |
paul@596 | 806 | |
paul@596 | 807 | # Update the context in the attribute details. |
paul@587 | 808 | |
paul@577 | 809 | else: |
paul@596 | 810 | emit(("__update_context", context_var, accessor)) |
paul@118 | 811 | |
paul@588 | 812 | # Omit the accessor for assignments and for invocations of static |
paul@588 | 813 | # targets. |
paul@588 | 814 | |
paul@588 | 815 | elif final_method not in ("assign", "static-assign", "static-invoke"): |
paul@103 | 816 | emit(accessor) |
paul@94 | 817 | |
paul@618 | 818 | # Produce an advisory instruction regarding the context. |
paul@618 | 819 | |
paul@618 | 820 | if context_var: |
paul@618 | 821 | emit(("<context_identity>", context_var)) |
paul@618 | 822 | |
paul@94 | 823 | self.access_instructions[access_location] = instructions |
paul@234 | 824 | self.accessor_kinds[access_location] = accessor_kinds |
paul@92 | 825 | |
paul@92 | 826 | def get_ambiguity_for_attributes(self, attrnames): |
paul@92 | 827 | |
paul@92 | 828 | """ |
paul@92 | 829 | Return a list of attribute position alternatives corresponding to each |
paul@92 | 830 | of the given 'attrnames'. |
paul@92 | 831 | """ |
paul@92 | 832 | |
paul@92 | 833 | ambiguity = [] |
paul@92 | 834 | |
paul@92 | 835 | for attrname in attrnames: |
paul@92 | 836 | position = self.attr_locations[attrname] |
paul@92 | 837 | ambiguity.append(len(self.locations[position])) |
paul@92 | 838 | |
paul@92 | 839 | return ambiguity |
paul@92 | 840 | |
paul@92 | 841 | def position_parameters(self): |
paul@92 | 842 | |
paul@92 | 843 | "Position the parameters for each function's parameter table." |
paul@92 | 844 | |
paul@641 | 845 | # Reverse the location mappings, producing a mapping from parameter |
paul@641 | 846 | # names to positions. |
paul@92 | 847 | |
paul@92 | 848 | param_locations = self.param_locations = {} |
paul@641 | 849 | self._position_attributes(param_locations, self.arg_locations) |
paul@92 | 850 | |
paul@92 | 851 | for name, argnames in self.importer.function_parameters.items(): |
paul@125 | 852 | |
paul@125 | 853 | # Allocate an extra context parameter in the table. |
paul@125 | 854 | |
paul@133 | 855 | l = self.parameters[name] = [None] + [None] * len(argnames) |
paul@92 | 856 | |
paul@92 | 857 | # Store an entry for the name along with the name's position in the |
paul@92 | 858 | # parameter list. |
paul@92 | 859 | |
paul@92 | 860 | for pos, argname in enumerate(argnames): |
paul@125 | 861 | |
paul@125 | 862 | # Position the argument in the table. |
paul@125 | 863 | |
paul@92 | 864 | position = param_locations[argname] |
paul@92 | 865 | if position >= len(l): |
paul@92 | 866 | l.extend([None] * (position - len(l) + 1)) |
paul@125 | 867 | |
paul@125 | 868 | # Indicate an argument list position starting from 1 (after the |
paul@125 | 869 | # initial context argument). |
paul@125 | 870 | |
paul@133 | 871 | l[position] = (argname, pos + 1) |
paul@92 | 872 | |
paul@643 | 873 | # Test the structure against any existing parameters. |
paul@643 | 874 | |
paul@643 | 875 | if self.existing_parameters: |
paul@643 | 876 | if self.existing_parameters.has_key(name) and self.existing_parameters[name] != l: |
paul@643 | 877 | self.differing_parameters.append(name) |
paul@643 | 878 | |
paul@92 | 879 | def populate_tables(self): |
paul@92 | 880 | |
paul@92 | 881 | """ |
paul@92 | 882 | Assign identifiers to attributes and encode structure information using |
paul@92 | 883 | these identifiers. |
paul@92 | 884 | """ |
paul@92 | 885 | |
paul@651 | 886 | # Initialise the mapping from attribute names to codes. |
paul@651 | 887 | |
paul@651 | 888 | l = self.all_attrnames = []; d = {} |
paul@651 | 889 | self._init_name_mapping(l, d, self.existing_attrnames) |
paul@651 | 890 | if self.indicated_attrnames: |
paul@651 | 891 | self._init_name_mapping(l, d, self.indicated_attrnames) |
paul@651 | 892 | self._update_name_mapping(l, d, self.attr_locations) |
paul@92 | 893 | |
paul@92 | 894 | # Record the numbers indicating the locations of the names. |
paul@92 | 895 | |
paul@92 | 896 | for key, attrnames in self.structures.items(): |
paul@92 | 897 | l = self.attr_table[key] = [] |
paul@92 | 898 | for attrname in attrnames: |
paul@92 | 899 | if attrname is None: |
paul@92 | 900 | l.append(None) |
paul@92 | 901 | else: |
paul@92 | 902 | l.append(d[attrname]) |
paul@92 | 903 | |
paul@651 | 904 | # Initialise the mapping from parameter names to codes. |
paul@651 | 905 | |
paul@651 | 906 | l = self.all_paramnames = []; d = {} |
paul@651 | 907 | self._init_name_mapping(l, d, self.existing_paramnames) |
paul@651 | 908 | if self.indicated_paramnames: |
paul@651 | 909 | self._init_name_mapping(l, d, self.indicated_paramnames) |
paul@651 | 910 | self._update_name_mapping(l, d, self.param_locations) |
paul@92 | 911 | |
paul@92 | 912 | # Record the numbers indicating the locations of the names. |
paul@92 | 913 | |
paul@92 | 914 | for key, values in self.parameters.items(): |
paul@92 | 915 | l = self.param_table[key] = [] |
paul@92 | 916 | for value in values: |
paul@92 | 917 | if value is None: |
paul@92 | 918 | l.append(None) |
paul@92 | 919 | else: |
paul@92 | 920 | name, pos = value |
paul@92 | 921 | l.append((d[name], pos)) |
paul@92 | 922 | |
paul@651 | 923 | def _init_name_mapping(self, l, d, existing): |
paul@92 | 924 | |
paul@92 | 925 | """ |
paul@651 | 926 | Initialise the name collection 'l', with mapping 'd', using the |
paul@651 | 927 | 'existing' mapping. |
paul@92 | 928 | """ |
paul@92 | 929 | |
paul@641 | 930 | i = 0 |
paul@651 | 931 | |
paul@651 | 932 | for name in existing: |
paul@651 | 933 | |
paul@651 | 934 | # Test for the name in another position. |
paul@651 | 935 | |
paul@651 | 936 | if d.has_key(name): |
paul@651 | 937 | if d[name] != i: |
paul@651 | 938 | raise OptimiseError, "Name %s has conflicting codes: %d and %d." % \ |
paul@651 | 939 | (name, d[name], i) |
paul@651 | 940 | else: |
paul@641 | 941 | |
paul@651 | 942 | # Test for other usage of the position. |
paul@641 | 943 | |
paul@651 | 944 | if i < len(l): |
paul@651 | 945 | if l[i] != name: |
paul@651 | 946 | raise OptimiseError, "Position %d has conflicting names: %s and %s." % \ |
paul@651 | 947 | (i, name, d[name]) |
paul@651 | 948 | l[i] = name |
paul@651 | 949 | else: |
paul@651 | 950 | l.append(name) |
paul@651 | 951 | |
paul@641 | 952 | d[name] = i |
paul@651 | 953 | |
paul@651 | 954 | i += 1 |
paul@651 | 955 | |
paul@651 | 956 | def _update_name_mapping(self, l, d, locations): |
paul@641 | 957 | |
paul@651 | 958 | """ |
paul@651 | 959 | Using any existing identifiers supplied by 'l' and 'd', update the |
paul@651 | 960 | identifiers using a sorted list of names from 'locations'. |
paul@651 | 961 | """ |
paul@641 | 962 | |
paul@651 | 963 | all_names = list(locations.keys()) |
paul@92 | 964 | all_names.sort() |
paul@641 | 965 | |
paul@651 | 966 | i = len(l) |
paul@651 | 967 | |
paul@641 | 968 | for name in all_names: |
paul@641 | 969 | if not d.has_key(name): |
paul@641 | 970 | d[name] = i |
paul@641 | 971 | l.append(name) |
paul@641 | 972 | i += 1 |
paul@641 | 973 | |
paul@92 | 974 | def populate_constants(self): |
paul@92 | 975 | |
paul@92 | 976 | """ |
paul@92 | 977 | Obtain a collection of distinct constant literals, building a mapping |
paul@92 | 978 | from constant references to those in this collection. |
paul@92 | 979 | """ |
paul@92 | 980 | |
paul@92 | 981 | # Obtain mappings from constant values to identifiers. |
paul@92 | 982 | |
paul@92 | 983 | self.constants = {} |
paul@92 | 984 | |
paul@92 | 985 | for path, constants in self.importer.all_constants.items(): |
paul@92 | 986 | |
paul@397 | 987 | # Record constants and obtain a number for them. |
paul@406 | 988 | # Each constant is actually (value, value_type, encoding). |
paul@92 | 989 | |
paul@397 | 990 | for constant, n in constants.items(): |
paul@620 | 991 | d = digest(constant) |
paul@620 | 992 | self.constants[constant] = d |
paul@620 | 993 | |
paul@620 | 994 | # Make sure the digests are really distinct for different |
paul@620 | 995 | # constants. |
paul@620 | 996 | |
paul@620 | 997 | if self.digests.has_key(d): |
paul@620 | 998 | if self.digests[d] != constant: |
paul@620 | 999 | raise OptimiseError, "Digest %s used for distinct constants %r and %r." % ( |
paul@620 | 1000 | d, self.digests[d], constant) |
paul@620 | 1001 | else: |
paul@620 | 1002 | self.digests[d] = constant |
paul@620 | 1003 | |
paul@620 | 1004 | # Establish a mapping from local constant identifiers to consolidated |
paul@620 | 1005 | # constant identifiers. |
paul@92 | 1006 | |
paul@92 | 1007 | self.constant_numbers = {} |
paul@92 | 1008 | |
paul@397 | 1009 | for name, constant in self.importer.all_constant_values.items(): |
paul@397 | 1010 | self.constant_numbers[name] = self.constants[constant] |
paul@92 | 1011 | |
paul@92 | 1012 | def combine_rows(a, b): |
paul@92 | 1013 | c = [] |
paul@92 | 1014 | for i, j in zip(a, b): |
paul@92 | 1015 | if i is None or j is None: |
paul@92 | 1016 | c.append(i or j) |
paul@92 | 1017 | else: |
paul@92 | 1018 | return None |
paul@92 | 1019 | return c |
paul@92 | 1020 | |
paul@92 | 1021 | def get_attributes_and_sizes(d): |
paul@92 | 1022 | |
paul@92 | 1023 | """ |
paul@640 | 1024 | Get attribute and size information for the object attributes defined by 'd' |
paul@640 | 1025 | providing a mapping from (object kind, type name) to attribute names. |
paul@640 | 1026 | |
paul@640 | 1027 | Return a matrix of attributes (each row entry consisting of column values |
paul@640 | 1028 | providing attribute names, with value positions corresponding to types |
paul@640 | 1029 | providing such attributes), a list of the type names corresponding to the |
paul@640 | 1030 | columns in the matrix, and a list of ranked sizes each indicating... |
paul@92 | 1031 | |
paul@92 | 1032 | * a weighted size depending on the kind of object |
paul@92 | 1033 | * the minimum size of an object employing an attribute |
paul@92 | 1034 | * the number of free columns in the matrix for the attribute |
paul@92 | 1035 | * the attribute name itself |
paul@92 | 1036 | """ |
paul@92 | 1037 | |
paul@92 | 1038 | attrs = {} |
paul@92 | 1039 | sizes = {} |
paul@564 | 1040 | objkinds = {} |
paul@92 | 1041 | |
paul@640 | 1042 | for objtype, attrnames in d.items(): |
paul@640 | 1043 | objkind, _name = objtype |
paul@92 | 1044 | |
paul@92 | 1045 | for attrname in attrnames: |
paul@92 | 1046 | |
paul@92 | 1047 | # Record each type supporting the attribute. |
paul@92 | 1048 | |
paul@92 | 1049 | init_item(attrs, attrname, set) |
paul@640 | 1050 | attrs[attrname].add(objtype) |
paul@92 | 1051 | |
paul@92 | 1052 | # Maintain a record of the smallest object size supporting the given |
paul@92 | 1053 | # attribute. |
paul@92 | 1054 | |
paul@92 | 1055 | if not sizes.has_key(attrname): |
paul@92 | 1056 | sizes[attrname] = len(attrnames) |
paul@92 | 1057 | else: |
paul@92 | 1058 | sizes[attrname] = min(sizes[attrname], len(attrnames)) |
paul@92 | 1059 | |
paul@92 | 1060 | # Record the object types/kinds supporting the attribute. |
paul@92 | 1061 | |
paul@564 | 1062 | init_item(objkinds, attrname, set) |
paul@564 | 1063 | objkinds[attrname].add(objkind) |
paul@92 | 1064 | |
paul@92 | 1065 | # Obtain attribute details in order of size and occupancy. |
paul@92 | 1066 | |
paul@640 | 1067 | all_objtypes = d.keys() |
paul@92 | 1068 | |
paul@92 | 1069 | rsizes = [] |
paul@92 | 1070 | for attrname, size in sizes.items(): |
paul@564 | 1071 | priority = "<instance>" in objkinds[attrname] and 0.5 or 1 |
paul@92 | 1072 | occupied = len(attrs[attrname]) |
paul@640 | 1073 | key = (priority * size, size, len(all_objtypes) - occupied, attrname) |
paul@92 | 1074 | rsizes.append(key) |
paul@92 | 1075 | |
paul@92 | 1076 | rsizes.sort() |
paul@92 | 1077 | |
paul@92 | 1078 | # Make a matrix of attributes. |
paul@92 | 1079 | |
paul@92 | 1080 | matrix = {} |
paul@640 | 1081 | for attrname, objtypes in attrs.items(): |
paul@640 | 1082 | |
paul@640 | 1083 | # Traverse the object types, adding the attribute name if the object |
paul@640 | 1084 | # type supports the attribute, adding None otherwise. |
paul@640 | 1085 | |
paul@92 | 1086 | row = [] |
paul@640 | 1087 | for objtype in all_objtypes: |
paul@640 | 1088 | if objtype in objtypes: |
paul@92 | 1089 | row.append(attrname) |
paul@92 | 1090 | else: |
paul@92 | 1091 | row.append(None) |
paul@92 | 1092 | matrix[attrname] = row |
paul@92 | 1093 | |
paul@640 | 1094 | return matrix, all_objtypes, rsizes |
paul@92 | 1095 | |
paul@92 | 1096 | def get_parameters_and_sizes(d): |
paul@92 | 1097 | |
paul@92 | 1098 | """ |
paul@92 | 1099 | Return a matrix of parameters, a list of functions corresponding to columns |
paul@92 | 1100 | in the matrix, and a list of ranked sizes each indicating... |
paul@92 | 1101 | |
paul@92 | 1102 | * a weighted size depending on the kind of object |
paul@92 | 1103 | * the minimum size of a parameter list employing a parameter |
paul@92 | 1104 | * the number of free columns in the matrix for the parameter |
paul@92 | 1105 | * the parameter name itself |
paul@92 | 1106 | |
paul@92 | 1107 | This is a slightly simpler version of the above 'get_attributes_and_sizes' |
paul@92 | 1108 | function. |
paul@92 | 1109 | """ |
paul@92 | 1110 | |
paul@92 | 1111 | params = {} |
paul@92 | 1112 | sizes = {} |
paul@92 | 1113 | |
paul@92 | 1114 | for name, argnames in d.items(): |
paul@92 | 1115 | for argname in argnames: |
paul@92 | 1116 | |
paul@92 | 1117 | # Record each function supporting the parameter. |
paul@92 | 1118 | |
paul@92 | 1119 | init_item(params, argname, set) |
paul@92 | 1120 | params[argname].add(name) |
paul@92 | 1121 | |
paul@92 | 1122 | # Maintain a record of the smallest parameter list supporting the |
paul@92 | 1123 | # given parameter. |
paul@92 | 1124 | |
paul@92 | 1125 | if not sizes.has_key(argname): |
paul@92 | 1126 | sizes[argname] = len(argnames) |
paul@92 | 1127 | else: |
paul@92 | 1128 | sizes[argname] = min(sizes[argname], len(argnames)) |
paul@92 | 1129 | |
paul@92 | 1130 | # Obtain attribute details in order of size and occupancy. |
paul@92 | 1131 | |
paul@92 | 1132 | names = d.keys() |
paul@92 | 1133 | |
paul@92 | 1134 | rsizes = [] |
paul@92 | 1135 | for argname, size in sizes.items(): |
paul@92 | 1136 | occupied = len(params[argname]) |
paul@92 | 1137 | key = (size, size, len(names) - occupied, argname) |
paul@92 | 1138 | rsizes.append(key) |
paul@92 | 1139 | |
paul@92 | 1140 | rsizes.sort() |
paul@92 | 1141 | |
paul@92 | 1142 | # Make a matrix of parameters. |
paul@92 | 1143 | |
paul@92 | 1144 | matrix = {} |
paul@92 | 1145 | for argname, types in params.items(): |
paul@92 | 1146 | row = [] |
paul@92 | 1147 | for name in names: |
paul@92 | 1148 | if name in types: |
paul@92 | 1149 | row.append(argname) |
paul@92 | 1150 | else: |
paul@92 | 1151 | row.append(None) |
paul@92 | 1152 | matrix[argname] = row |
paul@92 | 1153 | |
paul@92 | 1154 | return matrix, names, rsizes |
paul@92 | 1155 | |
paul@641 | 1156 | def get_allocated_locations(d, fn, existing=None): |
paul@92 | 1157 | |
paul@92 | 1158 | """ |
paul@92 | 1159 | Return a list where each element corresponds to a structure location and |
paul@92 | 1160 | contains a set of attribute names that may be stored at that location, given |
paul@564 | 1161 | a mapping 'd' whose keys are (object kind, object name) tuples and whose |
paul@92 | 1162 | values are collections of attributes. |
paul@92 | 1163 | """ |
paul@92 | 1164 | |
paul@92 | 1165 | matrix, names, rsizes = fn(d) |
paul@92 | 1166 | allocated = [] |
paul@92 | 1167 | |
paul@641 | 1168 | # Verify any existing allocation. |
paul@641 | 1169 | |
paul@641 | 1170 | allocated_attrnames = set() |
paul@641 | 1171 | |
paul@641 | 1172 | if existing: |
paul@641 | 1173 | for attrnames in existing: |
paul@641 | 1174 | |
paul@641 | 1175 | # Handle empty positions. |
paul@641 | 1176 | |
paul@641 | 1177 | if not attrnames: |
paul@650 | 1178 | allocated.append([None] * len(names)) |
paul@641 | 1179 | continue |
paul@641 | 1180 | |
paul@641 | 1181 | base = None |
paul@641 | 1182 | |
paul@641 | 1183 | for attrname in attrnames: |
paul@641 | 1184 | |
paul@641 | 1185 | # Skip presumably-removed attribute names. |
paul@641 | 1186 | |
paul@641 | 1187 | if not matrix.has_key(attrname): |
paul@641 | 1188 | continue |
paul@641 | 1189 | |
paul@641 | 1190 | # Handle the first attribute name. |
paul@641 | 1191 | |
paul@641 | 1192 | if base is None: |
paul@641 | 1193 | base = matrix[attrname] |
paul@641 | 1194 | allocated_attrnames.add(attrname) |
paul@641 | 1195 | continue |
paul@641 | 1196 | |
paul@641 | 1197 | # Combine existing and new attribute positioning. |
paul@641 | 1198 | |
paul@641 | 1199 | new = combine_rows(base, matrix[attrname]) |
paul@641 | 1200 | |
paul@641 | 1201 | if new: |
paul@641 | 1202 | base = new |
paul@641 | 1203 | allocated_attrnames.add(attrname) |
paul@647 | 1204 | else: |
paul@647 | 1205 | raise OptimiseError, "Attribute %s cannot be explicitly positioned at %d." % \ |
paul@647 | 1206 | (attrname, len(allocated)) |
paul@641 | 1207 | |
paul@650 | 1208 | # Handle empty positions. |
paul@650 | 1209 | |
paul@650 | 1210 | if base: |
paul@650 | 1211 | allocated.append(base) |
paul@650 | 1212 | else: |
paul@650 | 1213 | allocated.append([None] * len(names)) |
paul@641 | 1214 | |
paul@640 | 1215 | # Try to allocate each attribute name in turn. |
paul@640 | 1216 | |
paul@647 | 1217 | x = 0 |
paul@640 | 1218 | pos = 0 |
paul@640 | 1219 | |
paul@647 | 1220 | while x < len(rsizes): |
paul@647 | 1221 | |
paul@647 | 1222 | # Obtain any previous allocation at the current position. Start at the |
paul@647 | 1223 | # current attribute looking for allocations to combine. |
paul@640 | 1224 | |
paul@647 | 1225 | if pos < len(allocated): |
paul@647 | 1226 | base = allocated[pos] |
paul@647 | 1227 | free = base.count(None) |
paul@647 | 1228 | y = x |
paul@641 | 1229 | |
paul@640 | 1230 | # Obtain the object information for the attribute name. |
paul@640 | 1231 | |
paul@647 | 1232 | else: |
paul@647 | 1233 | weight, size, free, attrname = rsizes[x] |
paul@647 | 1234 | |
paul@647 | 1235 | # Ignore allocated attribute names. |
paul@647 | 1236 | |
paul@647 | 1237 | if attrname in allocated_attrnames: |
paul@647 | 1238 | x += 1 |
paul@647 | 1239 | continue |
paul@647 | 1240 | |
paul@647 | 1241 | # Start at the next attribute looking for allocations to combine. |
paul@647 | 1242 | |
paul@647 | 1243 | base = matrix[attrname] |
paul@647 | 1244 | y = x + 1 |
paul@640 | 1245 | |
paul@640 | 1246 | # Examine attribute names that follow in the ranking, attempting to |
paul@640 | 1247 | # accumulate compatible attributes that can co-exist in the same |
paul@640 | 1248 | # position within structures. |
paul@640 | 1249 | |
paul@92 | 1250 | while y < len(rsizes): |
paul@92 | 1251 | _weight, _size, _free, _attrname = rsizes[y] |
paul@640 | 1252 | |
paul@641 | 1253 | # Ignore allocated attribute names. |
paul@641 | 1254 | |
paul@641 | 1255 | if _attrname in allocated_attrnames: |
paul@641 | 1256 | y += 1 |
paul@641 | 1257 | continue |
paul@641 | 1258 | |
paul@640 | 1259 | # Determine whether this attribute is supported by too many types |
paul@640 | 1260 | # to co-exist. |
paul@640 | 1261 | |
paul@92 | 1262 | occupied = len(names) - _free |
paul@92 | 1263 | if occupied > free: |
paul@92 | 1264 | break |
paul@640 | 1265 | |
paul@640 | 1266 | # Merge the attribute support for both this and the currently |
paul@640 | 1267 | # considered attribute, testing for conflicts. Adopt the merged row |
paul@640 | 1268 | # details if they do not conflict. |
paul@640 | 1269 | |
paul@92 | 1270 | new = combine_rows(base, matrix[_attrname]) |
paul@92 | 1271 | if new: |
paul@92 | 1272 | del matrix[_attrname] |
paul@92 | 1273 | del rsizes[y] |
paul@92 | 1274 | base = new |
paul@92 | 1275 | free -= occupied |
paul@640 | 1276 | |
paul@640 | 1277 | # Otherwise, look for other compatible attributes. |
paul@640 | 1278 | |
paul@92 | 1279 | else: |
paul@92 | 1280 | y += 1 |
paul@640 | 1281 | |
paul@640 | 1282 | # Allocate the merged details at the current position. |
paul@640 | 1283 | |
paul@647 | 1284 | if pos < len(allocated): |
paul@647 | 1285 | allocated[pos] = base |
paul@647 | 1286 | pos += 1 |
paul@647 | 1287 | else: |
paul@647 | 1288 | x += 1 |
paul@647 | 1289 | allocated.append(base) |
paul@92 | 1290 | |
paul@641 | 1291 | return allocations_to_sets(allocated) |
paul@641 | 1292 | |
paul@641 | 1293 | def allocations_to_sets(allocated): |
paul@641 | 1294 | |
paul@641 | 1295 | """ |
paul@641 | 1296 | Return the list of attribute names from each row of the 'allocated' |
paul@641 | 1297 | attributes table. |
paul@641 | 1298 | """ |
paul@92 | 1299 | |
paul@130 | 1300 | locations = [] |
paul@641 | 1301 | |
paul@130 | 1302 | for attrnames in allocated: |
paul@130 | 1303 | l = set() |
paul@641 | 1304 | |
paul@641 | 1305 | # Convert populated allocations. |
paul@641 | 1306 | |
paul@641 | 1307 | if attrnames: |
paul@641 | 1308 | for attrname in attrnames: |
paul@641 | 1309 | if attrname: |
paul@641 | 1310 | l.add(attrname) |
paul@641 | 1311 | |
paul@130 | 1312 | locations.append(l) |
paul@641 | 1313 | |
paul@130 | 1314 | return locations |
paul@92 | 1315 | |
paul@92 | 1316 | # vim: tabstop=4 expandtab shiftwidth=4 |