1 #!/usr/bin/env python 2 3 """ 4 Optimise object layouts and generate access instruction plans. 5 6 Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from common import add_counter_item, get_attrname_from_location, init_item, \ 23 sorted_output 24 from encoders import encode_access_location, encode_instruction, get_kinds 25 from os.path import exists, join 26 from os import makedirs 27 from referencing import Reference 28 29 class Optimiser: 30 31 "Optimise objects in a program." 32 33 def __init__(self, importer, deducer, output): 34 35 """ 36 Initialise an instance using the given 'importer' and 'deducer' that 37 will perform the arrangement of attributes for program objects, writing 38 the results to the given 'output' directory. 39 """ 40 41 self.importer = importer 42 self.deducer = deducer 43 self.output = output 44 45 # Locations/offsets of attributes in objects. 46 47 self.locations = None 48 self.attr_locations = None 49 self.all_attrnames = None 50 51 # Locations of parameters in parameter tables. 52 53 self.arg_locations = None 54 self.param_locations = None 55 self.all_paramnames = None 56 57 # Specific attribute access information. 58 59 self.access_instructions = {} 60 61 # Object structure information. 62 63 self.structures = {} 64 self.attr_table = {} 65 66 # Parameter list information. 67 68 self.parameters = {} 69 self.param_table = {} 70 71 # Constant literal information. 72 73 self.constants = [] 74 self.constant_numbers = {} 75 76 # Optimiser activities. 77 78 self.populate_objects() 79 self.position_attributes() 80 self.populate_parameters() 81 self.position_parameters() 82 self.populate_tables() 83 self.populate_constants() 84 self.initialise_access_instructions() 85 86 def to_output(self): 87 88 "Write the output files using optimisation information." 89 90 if not exists(self.output): 91 makedirs(self.output) 92 93 self.write_objects() 94 95 def write_objects(self): 96 97 """ 98 Write object-related output. 99 100 The locations are a list of positions indicating the attributes residing 101 at each position in the different structures in a program. 102 103 ---- 104 105 The parameter locations are a list of positions indicating the parameters 106 residing at each position in the different parameter lists in a program. 107 108 ---- 109 110 Each attribute plan provides attribute details in the following format: 111 112 location " " name " " test " " test type " " base 113 " " traversed attributes " " traversed attribute ambiguity 114 " " traversal access modes 115 " " attributes to traverse " " attribute ambiguity 116 " " context " " access method " " static attribute 117 118 Locations have the following format: 119 120 qualified name of scope "." local name ":" name version 121 122 Traversal access modes are either "class" (obtain accessor class to 123 access attribute) or "object" (obtain attribute directly from accessor). 124 125 ---- 126 127 The structures are presented as a table in the following format: 128 129 qualified name " " attribute names 130 131 The attribute names are separated by ", " characters and indicate the 132 attribute provided at each position in the structure associated with the 133 given type. Where no attribute is provided at a particular location 134 within a structure, "-" is given. 135 136 ---- 137 138 The parameters are presented as a table in the following format: 139 140 qualified name " " parameter details 141 142 The parameter details are separated by ", " characters and indicate 143 the parameter name and list position for each parameter described at 144 each location in the parameter table associated with the given 145 function. Where no parameter details are provided at a particular 146 location within a parameter table, "-" is given. The name and list 147 position are separated by a colon (":"). 148 149 ---- 150 151 The attribute table is presented as a table in the following format: 152 153 qualified name " " attribute identifiers 154 155 Instead of attribute names, identifiers defined according to the order 156 given in the "attrnames" file are employed to denote the attributes 157 featured in each type's structure. Where no attribute is provided at a 158 particular location within a structure, "-" is given. 159 160 ---- 161 162 The parameter table is presented as a table in the following format: 163 164 qualified name " " parameter details 165 166 Instead of parameter names, identifiers defined according to the order 167 given in the "paramnames" file are employed to denote the parameters 168 featured in each function's parameter table. Where no parameter is 169 provided at a particular location within a table, "-" is given. 170 171 ---- 172 173 The ordered list of attribute names is given in the "attrnames" file. 174 175 ---- 176 177 The ordered list of parameter names is given in the "paramnames" file. 178 179 ---- 180 181 The ordered list of constant literals is given in the "constants" file. 182 """ 183 184 f = open(join(self.output, "locations"), "w") 185 try: 186 for attrnames in self.locations: 187 print >>f, sorted_output(attrnames) 188 189 finally: 190 f.close() 191 192 f = open(join(self.output, "parameter_locations"), "w") 193 try: 194 for argnames in self.arg_locations: 195 print >>f, sorted_output(argnames) 196 197 finally: 198 f.close() 199 200 f = open(join(self.output, "instruction_plans"), "w") 201 try: 202 access_instructions = self.access_instructions.items() 203 access_instructions.sort() 204 205 for location, instructions in access_instructions: 206 print >>f, encode_access_location(location), "..." 207 for instruction in instructions: 208 print >>f, encode_instruction(instruction) 209 print >>f 210 211 finally: 212 f.close() 213 214 f = open(join(self.output, "structures"), "w") 215 try: 216 structures = self.structures.items() 217 structures.sort() 218 219 for name, attrnames in structures: 220 print >>f, name, ", ".join([s or "-" for s in attrnames]) 221 222 finally: 223 f.close() 224 225 f = open(join(self.output, "parameters"), "w") 226 try: 227 parameters = self.parameters.items() 228 parameters.sort() 229 230 for name, argnames in parameters: 231 print >>f, name, ", ".join([s and ("%s:%d" % s) or "-" for s in argnames]) 232 233 finally: 234 f.close() 235 236 f = open(join(self.output, "attrtable"), "w") 237 try: 238 attr_table = self.attr_table.items() 239 attr_table.sort() 240 241 for name, attrcodes in attr_table: 242 print >>f, name, ", ".join([i is not None and str(i) or "-" for i in attrcodes]) 243 244 finally: 245 f.close() 246 247 f = open(join(self.output, "paramtable"), "w") 248 try: 249 param_table = self.param_table.items() 250 param_table.sort() 251 252 for name, paramcodes in param_table: 253 print >>f, name, ", ".join([s and ("%d:%d" % s) or "-" for s in paramcodes]) 254 255 finally: 256 f.close() 257 258 f = open(join(self.output, "attrnames"), "w") 259 try: 260 for name in self.all_attrnames: 261 print >>f, name 262 263 finally: 264 f.close() 265 266 f = open(join(self.output, "paramnames"), "w") 267 try: 268 for name in self.all_paramnames: 269 print >>f, name 270 271 finally: 272 f.close() 273 274 f = open(join(self.output, "constants"), "w") 275 try: 276 constants = [(n, value) for (value, n) in self.constants.items()] 277 constants.sort() 278 for n, value in constants: 279 print >>f, repr(value) 280 281 finally: 282 f.close() 283 284 def populate_objects(self): 285 286 "Populate objects using attribute and usage information." 287 288 all_attrs = {} 289 290 # Partition attributes into separate sections so that class and instance 291 # attributes are treated separately. 292 293 for source, objtype in [ 294 (self.importer.all_class_attrs, "<class>"), 295 (self.importer.all_instance_attrs, "<instance>"), 296 (self.importer.all_module_attrs, "<module>") 297 ]: 298 for name, attrs in source.items(): 299 all_attrs[(objtype, name)] = attrs 300 301 self.locations = get_allocated_locations(all_attrs, get_attributes_and_sizes) 302 303 def populate_parameters(self): 304 305 "Populate parameter tables using parameter information." 306 307 self.arg_locations = [set()] + get_allocated_locations(self.importer.function_parameters, get_parameters_and_sizes) 308 309 def position_attributes(self): 310 311 "Position specific attribute references." 312 313 # Reverse the location mappings. 314 315 attr_locations = self.attr_locations = {} 316 317 for i, attrnames in enumerate(self.locations): 318 for attrname in attrnames: 319 attr_locations[attrname] = i 320 321 # Record the structures. 322 323 for source, objtype in [ 324 (self.importer.all_class_attrs, "<class>"), 325 (self.importer.all_instance_attrs, "<instance>"), 326 (self.importer.all_module_attrs, "<module>") 327 ]: 328 329 for name, attrnames in source.items(): 330 key = Reference(objtype, name) 331 l = self.structures[key] = [None] * len(attrnames) 332 for attrname in attrnames: 333 position = attr_locations[attrname] 334 if position >= len(l): 335 l.extend([None] * (position - len(l) + 1)) 336 l[position] = attrname 337 338 def initialise_access_instructions(self): 339 340 "Expand access plans into instruction sequences." 341 342 for access_location, access_plan in self.deducer.access_plans.items(): 343 344 # Obtain the access details. 345 346 name, test, test_type, base, traversed, traversal_modes, \ 347 attrnames, context, context_test, first_method, final_method, origin = access_plan 348 349 instructions = [] 350 emit = instructions.append 351 352 if base: 353 original_accessor = base 354 else: 355 original_accessor = "<expr>" # use a generic placeholder 356 357 # Prepare for any first attribute access. 358 359 if traversed: 360 attrname = traversed[0] 361 del traversed[0] 362 elif attrnames: 363 attrname = attrnames[0] 364 del attrnames[0] 365 366 # Perform the first access explicitly if at least one operation 367 # requires it. 368 369 access_first_attribute = final_method in ("access", "assign") or traversed or attrnames 370 371 # Determine whether the first access involves assignment. 372 373 assigning = not traversed and not attrnames and final_method == "assign" 374 375 # Set the context if already available. 376 377 if context == "base": 378 accessor = context_var = (base,) 379 elif context == "original-accessor": 380 381 # Prevent re-evaluation of any dynamic expression by storing it. 382 383 if original_accessor == "<expr>": 384 emit(("__set_accessor", original_accessor)) 385 accessor = context_var = ("<accessor>",) 386 else: 387 accessor = context_var = (original_accessor,) 388 389 # Assigning does not set the context. 390 391 elif context in ("final-accessor", "unset") and access_first_attribute: 392 393 # Prevent re-evaluation of any dynamic expression by storing it. 394 395 if original_accessor == "<expr>": 396 emit(("__set_accessor", original_accessor)) 397 accessor = ("<accessor>",) 398 else: 399 accessor = (original_accessor,) 400 401 # Apply any test. 402 403 if test == "specific-type": 404 accessor = ("__test_specific_type", accessor, test_type) 405 elif test == "specific-instance": 406 accessor = ("__test_specific_instance", accessor, test_type) 407 elif test == "specific-object": 408 accessor = ("__test_specific_object", accessor, test_type) 409 elif test == "common-type": 410 accessor = ("__test_common_type", accessor, test_type) 411 elif test == "common-instance": 412 accessor = ("__test_common_instance", accessor, test_type) 413 elif test == "common-object": 414 accessor = ("__test_common_object", accessor, test_type) 415 416 # Perform the first or final access. 417 # The access only needs performing if the resulting accessor is used. 418 419 remaining = len(traversed + attrnames) 420 421 if access_first_attribute: 422 423 if first_method == "relative-class": 424 if assigning: 425 emit(("__store_via_class", accessor, attrname, "<assexpr>")) 426 else: 427 accessor = ("__load_via_class", accessor, attrname) 428 429 elif first_method == "relative-object": 430 if assigning: 431 emit(("__store_via_object", accessor, attrname, "<assexpr>")) 432 else: 433 accessor = ("__load_via_object", accessor, attrname) 434 435 elif first_method == "relative-object-class": 436 if assigning: 437 emit(("__get_class_and_store", accessor, attrname, "<assexpr>")) 438 else: 439 accessor = ("__get_class_and_load", accessor, attrname) 440 441 elif first_method == "check-class": 442 if assigning: 443 emit(("__check_and_store_via_class", accessor, attrname, "<assexpr>")) 444 else: 445 accessor = ("__check_and_load_via_class", accessor, attrname) 446 447 elif first_method == "check-object": 448 if assigning: 449 emit(("__check_and_store_via_object", accessor, attrname, "<assexpr>")) 450 else: 451 accessor = ("__check_and_load_via_object", accessor, attrname) 452 453 elif first_method == "check-object-class": 454 if assigning: 455 emit(("__check_and_store_via_any", accessor, attrname, "<assexpr>")) 456 else: 457 accessor = ("__check_and_load_via_any", accessor, attrname) 458 459 # Traverse attributes using the accessor. 460 461 if traversed: 462 for attrname, traversal_mode in zip(traversed, traversal_modes): 463 assigning = remaining == 1 and final_method == "assign" 464 465 # Set the context, if appropriate. 466 467 if remaining == 1 and final_method != "assign" and context == "final-accessor": 468 emit(("__set_context", accessor)) 469 accessor = context_var = "<context>" 470 471 # Perform the access only if not achieved directly. 472 473 if remaining > 1 or final_method in ("access", "assign"): 474 475 if traversal_mode == "class": 476 if assigning: 477 emit(("__store_via_class", accessor, attrname, "<assexpr>")) 478 else: 479 accessor = ("__load_via_class", accessor, attrname) 480 else: 481 if assigning: 482 emit(("__store_via_object", accessor, attrname, "<assexpr>")) 483 else: 484 accessor = ("__load_via_object", accessor, attrname) 485 486 remaining -= 1 487 488 if attrnames: 489 for attrname in attrnames: 490 assigning = remaining == 1 and final_method == "assign" 491 492 # Set the context, if appropriate. 493 494 if remaining == 1 and final_method != "assign" and context == "final-accessor": 495 emit(("__set_context", accessor)) 496 accessor = context_var = "<context>" 497 498 # Perform the access only if not achieved directly. 499 500 if remaining > 1 or final_method in ("access", "assign"): 501 502 if assigning: 503 emit(("__check_and_store_via_any", accessor, attrname, "<assexpr>")) 504 else: 505 accessor = ("__check_and_load_via_any", accessor, attrname) 506 507 remaining -= 1 508 509 # Define or emit the means of accessing the actual target. 510 511 if final_method == "static-assign": 512 parent, attrname = origin.rsplit(".", 1) 513 emit(("__store_via_object", parent, attrname, "<assexpr>")) 514 515 elif final_method in ("static", "static-invoke"): 516 parent, attrname = origin.rsplit(".", 1) 517 accessor = ("__load_static", parent, origin) 518 519 # Wrap accesses in context operations. 520 521 if context_test == "test": 522 emit(("__test_context", context_var, accessor)) 523 524 elif context_test == "replace": 525 526 # Static invocation targets have a context added but no other 527 # transformation performed. 528 529 if final_method == "static-invoke": 530 emit(("__update_context", context_var, accessor)) 531 532 # Other invocation targets gain a context and have the bound 533 # version of the callable activated. 534 535 else: 536 emit(("__replace_context", context_var, accessor)) 537 538 elif final_method not in ("assign", "static-assign"): 539 emit(accessor) 540 541 self.access_instructions[access_location] = instructions 542 543 def get_ambiguity_for_attributes(self, attrnames): 544 545 """ 546 Return a list of attribute position alternatives corresponding to each 547 of the given 'attrnames'. 548 """ 549 550 ambiguity = [] 551 552 for attrname in attrnames: 553 position = self.attr_locations[attrname] 554 ambiguity.append(len(self.locations[position])) 555 556 return ambiguity 557 558 def position_parameters(self): 559 560 "Position the parameters for each function's parameter table." 561 562 # Reverse the location mappings. 563 564 param_locations = self.param_locations = {} 565 566 for i, argnames in enumerate(self.arg_locations): 567 568 # Position the arguments. 569 570 for argname in argnames: 571 param_locations[argname] = i 572 573 for name, argnames in self.importer.function_parameters.items(): 574 575 # Allocate an extra context parameter in the table. 576 577 l = self.parameters[name] = [None] + [None] * len(argnames) 578 579 # Store an entry for the name along with the name's position in the 580 # parameter list. 581 582 for pos, argname in enumerate(argnames): 583 584 # Position the argument in the table. 585 586 position = param_locations[argname] 587 if position >= len(l): 588 l.extend([None] * (position - len(l) + 1)) 589 590 # Indicate an argument list position starting from 1 (after the 591 # initial context argument). 592 593 l[position] = (argname, pos + 1) 594 595 def populate_tables(self): 596 597 """ 598 Assign identifiers to attributes and encode structure information using 599 these identifiers. 600 """ 601 602 self.all_attrnames, d = self._get_name_mapping(self.attr_locations) 603 604 # Record the numbers indicating the locations of the names. 605 606 for key, attrnames in self.structures.items(): 607 l = self.attr_table[key] = [] 608 for attrname in attrnames: 609 if attrname is None: 610 l.append(None) 611 else: 612 l.append(d[attrname]) 613 614 self.all_paramnames, d = self._get_name_mapping(self.param_locations) 615 616 # Record the numbers indicating the locations of the names. 617 618 for key, values in self.parameters.items(): 619 l = self.param_table[key] = [] 620 for value in values: 621 if value is None: 622 l.append(None) 623 else: 624 name, pos = value 625 l.append((d[name], pos)) 626 627 def _get_name_mapping(self, locations): 628 629 """ 630 Get a sorted list of names from 'locations', then map them to 631 identifying numbers. Return the list and the mapping. 632 """ 633 634 all_names = locations.keys() 635 all_names.sort() 636 return all_names, dict([(name, i) for i, name in enumerate(all_names)]) 637 638 def populate_constants(self): 639 640 """ 641 Obtain a collection of distinct constant literals, building a mapping 642 from constant references to those in this collection. 643 """ 644 645 # Obtain mappings from constant values to identifiers. 646 647 self.constants = {} 648 649 for path, constants in self.importer.all_constants.items(): 650 for constant, n in constants.items(): 651 652 # Record constants and obtain a number for them. 653 654 add_counter_item(self.constants, constant) 655 656 self.constant_numbers = {} 657 658 for name, (value, value_type) in self.importer.all_constant_values.items(): 659 self.constant_numbers[name] = self.constants[value] 660 661 def combine_rows(a, b): 662 c = [] 663 for i, j in zip(a, b): 664 if i is None or j is None: 665 c.append(i or j) 666 else: 667 return None 668 return c 669 670 def get_attributes_and_sizes(d): 671 672 """ 673 Return a matrix of attributes, a list of type names corresponding to columns 674 in the matrix, and a list of ranked sizes each indicating... 675 676 * a weighted size depending on the kind of object 677 * the minimum size of an object employing an attribute 678 * the number of free columns in the matrix for the attribute 679 * the attribute name itself 680 """ 681 682 attrs = {} 683 sizes = {} 684 objtypes = {} 685 686 for name, attrnames in d.items(): 687 objtype, _name = name 688 689 for attrname in attrnames: 690 691 # Record each type supporting the attribute. 692 693 init_item(attrs, attrname, set) 694 attrs[attrname].add(name) 695 696 # Maintain a record of the smallest object size supporting the given 697 # attribute. 698 699 if not sizes.has_key(attrname): 700 sizes[attrname] = len(attrnames) 701 else: 702 sizes[attrname] = min(sizes[attrname], len(attrnames)) 703 704 # Record the object types/kinds supporting the attribute. 705 706 init_item(objtypes, attrname, set) 707 objtypes[attrname].add(objtype) 708 709 # Obtain attribute details in order of size and occupancy. 710 711 names = d.keys() 712 713 rsizes = [] 714 for attrname, size in sizes.items(): 715 priority = "<instance>" in objtypes[attrname] and 0.5 or 1 716 occupied = len(attrs[attrname]) 717 key = (priority * size, size, len(names) - occupied, attrname) 718 rsizes.append(key) 719 720 rsizes.sort() 721 722 # Make a matrix of attributes. 723 724 matrix = {} 725 for attrname, types in attrs.items(): 726 row = [] 727 for name in names: 728 if name in types: 729 row.append(attrname) 730 else: 731 row.append(None) 732 matrix[attrname] = row 733 734 return matrix, names, rsizes 735 736 def get_parameters_and_sizes(d): 737 738 """ 739 Return a matrix of parameters, a list of functions corresponding to columns 740 in the matrix, and a list of ranked sizes each indicating... 741 742 * a weighted size depending on the kind of object 743 * the minimum size of a parameter list employing a parameter 744 * the number of free columns in the matrix for the parameter 745 * the parameter name itself 746 747 This is a slightly simpler version of the above 'get_attributes_and_sizes' 748 function. 749 """ 750 751 params = {} 752 sizes = {} 753 754 for name, argnames in d.items(): 755 for argname in argnames: 756 757 # Record each function supporting the parameter. 758 759 init_item(params, argname, set) 760 params[argname].add(name) 761 762 # Maintain a record of the smallest parameter list supporting the 763 # given parameter. 764 765 if not sizes.has_key(argname): 766 sizes[argname] = len(argnames) 767 else: 768 sizes[argname] = min(sizes[argname], len(argnames)) 769 770 # Obtain attribute details in order of size and occupancy. 771 772 names = d.keys() 773 774 rsizes = [] 775 for argname, size in sizes.items(): 776 occupied = len(params[argname]) 777 key = (size, size, len(names) - occupied, argname) 778 rsizes.append(key) 779 780 rsizes.sort() 781 782 # Make a matrix of parameters. 783 784 matrix = {} 785 for argname, types in params.items(): 786 row = [] 787 for name in names: 788 if name in types: 789 row.append(argname) 790 else: 791 row.append(None) 792 matrix[argname] = row 793 794 return matrix, names, rsizes 795 796 def get_allocated_locations(d, fn): 797 798 """ 799 Return a list where each element corresponds to a structure location and 800 contains a set of attribute names that may be stored at that location, given 801 a mapping 'd' whose keys are (object type, object name) tuples and whose 802 values are collections of attributes. 803 """ 804 805 matrix, names, rsizes = fn(d) 806 allocated = [] 807 808 x = 0 809 while x < len(rsizes): 810 weight, size, free, attrname = rsizes[x] 811 base = matrix[attrname] 812 y = x + 1 813 while y < len(rsizes): 814 _weight, _size, _free, _attrname = rsizes[y] 815 occupied = len(names) - _free 816 if occupied > free: 817 break 818 new = combine_rows(base, matrix[_attrname]) 819 if new: 820 del matrix[_attrname] 821 del rsizes[y] 822 base = new 823 free -= occupied 824 else: 825 y += 1 826 allocated.append(base) 827 x += 1 828 829 # Return the list of attribute names from each row of the allocated 830 # attributes table. 831 832 locations = [] 833 for attrnames in allocated: 834 l = set() 835 for attrname in attrnames: 836 if attrname: 837 l.add(attrname) 838 locations.append(l) 839 return locations 840 841 # vim: tabstop=4 expandtab shiftwidth=4