1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/access_plan.py Tue Sep 05 21:16:49 2023 +0200
1.3 @@ -0,0 +1,545 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Attribute access plan translation.
1.8 +
1.9 +Copyright (C) 2014-2018, 2023 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from encoders import encode_access_location
1.26 +
1.27 +class AccessPlan:
1.28 +
1.29 + "An attribute access plan."
1.30 +
1.31 + def __init__(self, name, test, test_type, base, traversed, traversal_modes,
1.32 + remaining, context, context_test, first_method, final_method,
1.33 + origin, accessor_kinds):
1.34 +
1.35 + "Initialise the plan."
1.36 +
1.37 + # With instance attribute initialisers, the assignment below would be
1.38 + # generated automatically.
1.39 +
1.40 + (
1.41 + self.name, self.test, self.test_type, self.base,
1.42 + self.traversed, self.traversal_modes, self.remaining,
1.43 + self.context, self.context_test,
1.44 + self.first_method, self.final_method,
1.45 + self.origin, self.accessor_kinds) = (
1.46 +
1.47 + name, test, test_type, base,
1.48 + traversed, traversal_modes, remaining,
1.49 + context, context_test,
1.50 + first_method, final_method,
1.51 + origin, accessor_kinds)
1.52 +
1.53 + # Define the first attribute access and subsequent accesses.
1.54 +
1.55 + self.first_attrname = None
1.56 + self.traversed_attrnames = traversed
1.57 + self.traversed_attrname_modes = traversal_modes
1.58 + self.remaining_attrnames = remaining
1.59 +
1.60 + if traversed:
1.61 + self.first_attrname = traversed[0]
1.62 + self.traversed_attrnames = traversed[1:]
1.63 + self.traversed_attrname_modes = traversal_modes[1:]
1.64 + elif remaining:
1.65 + self.first_attrname = remaining[0]
1.66 + self.remaining_attrnames = remaining[1:]
1.67 +
1.68 + def access_first_attribute(self):
1.69 +
1.70 + "Return whether the first attribute is to be accessed."
1.71 +
1.72 + return self.final_method in ("access", "access-invoke", "assign") or \
1.73 + self.all_subsequent_attributes()
1.74 +
1.75 + def assigning_first_attribute(self):
1.76 +
1.77 + "Return whether the first attribute access involves assignment."
1.78 +
1.79 + return not self.all_subsequent_attributes() and self.final_method == "assign"
1.80 +
1.81 + def get_first_attribute_name(self):
1.82 +
1.83 + "Return any first attribute name to be used in an initial access."
1.84 +
1.85 + return self.first_attrname
1.86 +
1.87 + def all_subsequent_attributes(self):
1.88 +
1.89 + "Return all subsequent attribute names involved in accesses."
1.90 +
1.91 + return self.traversed_attrnames + self.remaining_attrnames
1.92 +
1.93 + def attribute_traversals(self):
1.94 +
1.95 + "Return a collection of (attribute name, traversal mode) tuples."
1.96 +
1.97 + return zip(self.traversed_attrnames, self.traversed_attrname_modes)
1.98 +
1.99 + def stored_accessor(self):
1.100 +
1.101 + "Return the variable used to obtain the accessor."
1.102 +
1.103 + return self.assigning_first_attribute() and "<target_accessor>" or "<accessor>"
1.104 +
1.105 + def stored_accessor_modifier(self):
1.106 +
1.107 + "Return the variable used to set the accessor."
1.108 +
1.109 + return self.assigning_first_attribute() and "<set_target_accessor>" or "<set_accessor>"
1.110 +
1.111 + def get_original_accessor(self):
1.112 +
1.113 + "Return the original accessor details."
1.114 +
1.115 + # Identify any static original accessor.
1.116 +
1.117 + if self.base:
1.118 + return self.base
1.119 +
1.120 + # Employ names as contexts unless the context needs testing and
1.121 + # potentially updating. In such cases, temporary context storage is
1.122 + # used instead.
1.123 +
1.124 + elif self.name and not (self.context_test == "test" and
1.125 + self.final_method in ("access-invoke", "static-invoke")):
1.126 +
1.127 + return "<name>"
1.128 +
1.129 + # Use a generic placeholder representing the access expression in
1.130 + # the general case.
1.131 +
1.132 + else:
1.133 + return "<expr>"
1.134 +
1.135 + def get_instructions(self):
1.136 +
1.137 + "Return a list of instructions corresponding to the plan."
1.138 +
1.139 + # Emit instructions by appending them to a list.
1.140 +
1.141 + instructions = []
1.142 + emit = instructions.append
1.143 +
1.144 + # Set up any initial instructions.
1.145 +
1.146 + accessor, context = self.process_initialisation(emit)
1.147 +
1.148 + # Apply any test.
1.149 +
1.150 + if self.test[0] == "test":
1.151 + test_accessor = accessor = ("__%s_%s_%s" % self.test, accessor, self.test_type)
1.152 + else:
1.153 + test_accessor = None
1.154 +
1.155 + # Perform the first or final access.
1.156 + # The access only needs performing if the resulting accessor is used.
1.157 +
1.158 + accessor = self.process_first_attribute(accessor, emit)
1.159 +
1.160 + # Perform accesses for the traversed and remaining attributes.
1.161 +
1.162 + accessor, context = self.process_traversed_attributes(accessor, context, emit)
1.163 + accessor, context = self.process_remaining_attributes(accessor, context, emit)
1.164 +
1.165 + # Make any accessor test available if not emitted.
1.166 +
1.167 + test_accessor = not instructions and test_accessor or None
1.168 +
1.169 + # Perform the access on the actual target.
1.170 +
1.171 + accessor, context = self.process_attribute_access(accessor, context, test_accessor, emit)
1.172 +
1.173 + # Produce an advisory instruction regarding the context.
1.174 +
1.175 + self.process_context_identity(context, emit)
1.176 +
1.177 + # Produce an advisory instruction regarding the final attribute.
1.178 +
1.179 + if self.origin:
1.180 + emit(("<final_identity>", self.origin))
1.181 +
1.182 + return instructions
1.183 +
1.184 + def process_initialisation(self, emit):
1.185 +
1.186 + """
1.187 + Use 'emit' to generate instructions for any initialisation of attribute
1.188 + access. Return the potentially revised accessor and context indicators.
1.189 + """
1.190 +
1.191 + # Identify any static original accessor.
1.192 +
1.193 + original_accessor = self.get_original_accessor()
1.194 +
1.195 + # Determine whether the first access involves assignment.
1.196 +
1.197 + set_accessor = self.stored_accessor_modifier()
1.198 + stored_accessor = self.stored_accessor()
1.199 +
1.200 + # Set the context if already available.
1.201 +
1.202 + context = None
1.203 +
1.204 + if self.context == "base":
1.205 + accessor = context = (self.base,)
1.206 + elif self.context == "original-accessor":
1.207 +
1.208 + # Prevent re-evaluation of any dynamic expression by storing it.
1.209 +
1.210 + if original_accessor == "<expr>":
1.211 + if self.final_method in ("access-invoke", "static-invoke"):
1.212 + emit(("<set_context>", original_accessor))
1.213 + accessor = context = ("<context>",)
1.214 + else:
1.215 + emit((set_accessor, original_accessor))
1.216 + accessor = context = (stored_accessor,)
1.217 + else:
1.218 + accessor = context = (original_accessor,)
1.219 +
1.220 + # Assigning does not set the context.
1.221 +
1.222 + elif self.context in ("final-accessor", "unset") and self.access_first_attribute():
1.223 +
1.224 + # Prevent re-evaluation of any dynamic expression by storing it.
1.225 +
1.226 + if original_accessor == "<expr>":
1.227 + emit((set_accessor, original_accessor))
1.228 + accessor = (stored_accessor,)
1.229 + else:
1.230 + accessor = (original_accessor,)
1.231 + else:
1.232 + accessor = None
1.233 +
1.234 + return accessor, context
1.235 +
1.236 + def process_first_attribute(self, accessor, emit):
1.237 +
1.238 + """
1.239 + Using 'accessor', use 'emit' to generate instructions for any first
1.240 + attribute access. Return the potentially revised accessor.
1.241 + """
1.242 +
1.243 + if self.access_first_attribute():
1.244 + attrname = self.get_first_attribute_name()
1.245 + assigning = self.assigning_first_attribute()
1.246 +
1.247 + store = attrname != "__data__" and "__store_via_attr_ref" or "__store_via_attr_ref__"
1.248 +
1.249 + # Access via the accessor's class.
1.250 +
1.251 + if self.first_method == "relative-class":
1.252 + if assigning:
1.253 + emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
1.254 + emit((store, "<attr_ref>", "<assexpr>"))
1.255 + else:
1.256 + accessor = ("__load_via_class", accessor, attrname)
1.257 +
1.258 + # Access via the accessor itself.
1.259 +
1.260 + elif self.first_method == "relative-object":
1.261 + if assigning:
1.262 + emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
1.263 + emit((store, "<attr_ref>", "<assexpr>"))
1.264 + else:
1.265 + accessor = ("__load_via_object", accessor, attrname)
1.266 +
1.267 + # Access via a class accessor or the accessor's class.
1.268 +
1.269 + elif self.first_method == "relative-object-class":
1.270 + if assigning:
1.271 + emit(("__raise_type_error",))
1.272 + else:
1.273 + accessor = ("__get_class_and_load", accessor, attrname)
1.274 +
1.275 + # Access via the accessor's class.
1.276 +
1.277 + elif self.first_method == "check-class":
1.278 + if assigning:
1.279 + emit(("__raise_type_error",))
1.280 + else:
1.281 + accessor = ("__check_and_load_via_class", accessor, attrname)
1.282 +
1.283 + # Access via the accessor itself.
1.284 +
1.285 + elif self.first_method == "check-object":
1.286 + if assigning:
1.287 + emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
1.288 + emit((store, "<attr_ref>", "<assexpr>"))
1.289 + else:
1.290 + accessor = ("__check_and_load_via_object", accessor, attrname)
1.291 +
1.292 + # Access via a class accessor or the accessor's class.
1.293 + # Here, only access via the accessor is supported.
1.294 +
1.295 + elif self.first_method == "check-object-class":
1.296 + if assigning:
1.297 + emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
1.298 + emit((store, "<attr_ref>", "<assexpr>"))
1.299 + else:
1.300 + accessor = ("__check_and_load_via_any", accessor, attrname)
1.301 +
1.302 + return accessor
1.303 +
1.304 + def process_traversed_attributes(self, accessor, context, emit):
1.305 +
1.306 + """
1.307 + Using 'accessor' and 'context', use 'emit' to generate instructions
1.308 + for the traversed attribute accesses. Return the potentially revised
1.309 + accessor and context indicators.
1.310 + """
1.311 +
1.312 + # Traverse attributes using the accessor.
1.313 +
1.314 + num_remaining = len(self.all_subsequent_attributes())
1.315 +
1.316 + if self.traversed_attrnames:
1.317 + for attrname, traversal_mode in self.attribute_traversals():
1.318 + assigning = num_remaining == 1 and self.final_method == "assign"
1.319 +
1.320 + # Set the context, if appropriate.
1.321 +
1.322 + if num_remaining == 1 and self.final_method != "assign" and self.context == "final-accessor":
1.323 +
1.324 + # Invoked attributes employ a separate context accessed
1.325 + # during invocation.
1.326 +
1.327 + if self.final_method in ("access-invoke", "static-invoke"):
1.328 + emit(("<set_context>", accessor))
1.329 + accessor = context = "<context>"
1.330 +
1.331 + # A private context within the access is otherwise
1.332 + # retained.
1.333 +
1.334 + else:
1.335 + emit(("<set_private_context>", accessor))
1.336 + accessor = context = "<private_context>"
1.337 +
1.338 + # Perform the access only if not achieved directly.
1.339 +
1.340 + if num_remaining > 1 or self.final_method in ("access", "access-invoke", "assign"):
1.341 +
1.342 + store = attrname != "__data__" and "__store_via_attr_ref" or "__store_via_attr_ref__"
1.343 +
1.344 + if traversal_mode == "class":
1.345 + if assigning:
1.346 + emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
1.347 + emit((store, "<attr_ref>", "<assexpr>"))
1.348 + else:
1.349 + accessor = ("__load_via_class", accessor, attrname)
1.350 + else:
1.351 + if assigning:
1.352 + emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
1.353 + emit((store, "<attr_ref>", "<assexpr>"))
1.354 + else:
1.355 + accessor = ("__load_via_object", accessor, attrname)
1.356 +
1.357 + num_remaining -= 1
1.358 +
1.359 + return accessor, context
1.360 +
1.361 + def process_remaining_attributes(self, accessor, context, emit):
1.362 +
1.363 + """
1.364 + Using 'accessor' and 'context', use 'emit' to generate instructions
1.365 + for the remaining attribute accesses. Return the potentially revised
1.366 + accessor and context indicators.
1.367 + """
1.368 +
1.369 + remaining = self.remaining_attrnames
1.370 +
1.371 + if remaining:
1.372 + num_remaining = len(remaining)
1.373 +
1.374 + for attrname in remaining:
1.375 + assigning = num_remaining == 1 and self.final_method == "assign"
1.376 +
1.377 + # Set the context, if appropriate.
1.378 +
1.379 + if num_remaining == 1 and self.final_method != "assign" and self.context == "final-accessor":
1.380 +
1.381 + # Invoked attributes employ a separate context accessed
1.382 + # during invocation.
1.383 +
1.384 + if self.final_method in ("access-invoke", "static-invoke"):
1.385 + emit(("<set_context>", accessor))
1.386 + accessor = context = "<context>"
1.387 +
1.388 + # A private context within the access is otherwise
1.389 + # retained.
1.390 +
1.391 + else:
1.392 + emit(("<set_private_context>", accessor))
1.393 + accessor = context = "<private_context>"
1.394 +
1.395 + # Perform the access only if not achieved directly.
1.396 +
1.397 + if num_remaining > 1 or self.final_method in ("access", "access-invoke", "assign"):
1.398 +
1.399 + # Constrain instructions involving certain special
1.400 + # attribute names.
1.401 +
1.402 + store = attrname != "__data__" and "__store_via_attr_ref" or "__store_via_attr_ref__"
1.403 + to_search = attrname != "__data__" and "any" or "object"
1.404 +
1.405 + if assigning:
1.406 + emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
1.407 + emit((store, "<attr_ref>", "<assexpr>"))
1.408 + else:
1.409 + accessor = ("__check_and_load_via_%s" % to_search, accessor, attrname)
1.410 +
1.411 + num_remaining -= 1
1.412 +
1.413 + return accessor, context
1.414 +
1.415 + def process_attribute_access(self, accessor, context, test_accessor, emit):
1.416 +
1.417 + """
1.418 + Using 'accessor','context' and any 'test_accessor' operation, use 'emit'
1.419 + to generate instructions for the final attribute access. Return the
1.420 + potentially revised accessor and context indicators.
1.421 + """
1.422 +
1.423 + # Define any replacement context variable plus the default eventual
1.424 + # context variable.
1.425 +
1.426 + replacement_context = "<new_context>"
1.427 + final_context = context
1.428 +
1.429 + # Define or emit the means of accessing the actual target.
1.430 +
1.431 + if self.final_method in ("static", "static-assign", "static-invoke"):
1.432 +
1.433 + if test_accessor:
1.434 + emit(test_accessor)
1.435 +
1.436 + # Assignments to known attributes.
1.437 +
1.438 + if self.final_method == "static-assign":
1.439 + parent, attrname = self.origin.rsplit(".", 1)
1.440 + store = attrname != "__data__" and "__store_via_attr_ref" or "__store_via_attr_ref__"
1.441 + emit(("<set_attr_ref>", ("__get_object_attr_ref", parent, attrname)))
1.442 + emit((store, "<attr_ref>", "<assexpr>"))
1.443 +
1.444 + # Invoked attributes employ a separate context.
1.445 +
1.446 + elif self.final_method in ("static", "static-invoke"):
1.447 + accessor = ("__load_static_ignore", self.origin)
1.448 +
1.449 + # Wrap accesses in context operations.
1.450 +
1.451 + if self.context_test == "test":
1.452 +
1.453 + # Test and combine the context with static attribute details.
1.454 +
1.455 + if self.final_method == "static":
1.456 + emit(("__load_static_test", replacement_context, context, self.origin))
1.457 + final_context = replacement_context
1.458 +
1.459 + # Test the context, storing it separately if required for the
1.460 + # immediately invoked static attribute.
1.461 +
1.462 + elif self.final_method == "static-invoke":
1.463 + emit(("<test_context_static>", context, self.origin))
1.464 +
1.465 + # Test the context, storing it separately if required for an
1.466 + # immediately invoked attribute.
1.467 +
1.468 + elif self.final_method == "access-invoke":
1.469 + emit(("<test_context_revert>", context, accessor))
1.470 +
1.471 + # Test the context and update the attribute details if
1.472 + # appropriate.
1.473 +
1.474 + else:
1.475 + emit(("__test_context", replacement_context, context, accessor))
1.476 + final_context = replacement_context
1.477 +
1.478 + elif self.context_test == "replace":
1.479 +
1.480 + # Produce an object with updated context.
1.481 +
1.482 + if self.final_method == "static":
1.483 + emit(("__load_static_replace", replacement_context, context, self.origin))
1.484 + final_context = replacement_context
1.485 +
1.486 + # Omit the context update operation where the target is static
1.487 + # and the context is recorded separately.
1.488 +
1.489 + elif self.final_method == "static-invoke":
1.490 + pass
1.491 +
1.492 + # If a separate context is used for an immediate invocation,
1.493 + # produce the attribute details unchanged.
1.494 +
1.495 + elif self.final_method == "access-invoke":
1.496 + emit(accessor)
1.497 +
1.498 + # Update the context in the attribute details.
1.499 +
1.500 + else:
1.501 + emit(("__update_context", replacement_context, context, accessor))
1.502 + final_context = replacement_context
1.503 +
1.504 + # Omit the accessor for assignments and for invocations of static
1.505 + # targets. Otherwise, emit the accessor which may involve the
1.506 + # invocation of a test.
1.507 +
1.508 + elif self.final_method not in ("assign", "static-assign", "static-invoke"):
1.509 + emit(accessor)
1.510 +
1.511 + return accessor, final_context
1.512 +
1.513 + def process_context_identity(self, context, emit):
1.514 +
1.515 + """
1.516 + Using 'context', use 'emit' to generate instructions to test the context
1.517 + identity.
1.518 + """
1.519 +
1.520 + if context:
1.521 +
1.522 + # Only verify the context for invocation purposes if a suitable
1.523 + # test has been performed.
1.524 +
1.525 + if self.context_test in ("ignore", "replace") or \
1.526 + self.final_method in ("access-invoke", "static-invoke"):
1.527 +
1.528 + emit(("<context_identity_verified>", context))
1.529 + else:
1.530 + emit(("<context_identity>", context))
1.531 +
1.532 + def write(self, f, location):
1.533 +
1.534 + "Write the plan to file 'f' with the given 'location' information."
1.535 +
1.536 + print >>f, encode_access_location(location), \
1.537 + self.name or "{}", \
1.538 + self.test and "-".join(self.test) or "{}", \
1.539 + self.test_type or "{}", \
1.540 + self.base or "{}", \
1.541 + ".".join(self.traversed_attrnames) or "{}", \
1.542 + ".".join(self.traversed_attrname_modes) or "{}", \
1.543 + ".".join(self.remaining_attrnames) or "{}", \
1.544 + self.context, self.context_test, \
1.545 + self.first_method, self.final_method, self.origin or "{}", \
1.546 + ",".join(self.accessor_kinds)
1.547 +
1.548 +# vim: tabstop=4 expandtab shiftwidth=4