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