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