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