1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/access_plan.py Mon Sep 04 01:23:39 2023 +0200
1.3 @@ -0,0 +1,508 @@
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 = 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 + if self.first_method == "relative-class":
1.248 + if assigning:
1.249 + emit(("__store_via_class", accessor, attrname, "<assexpr>"))
1.250 + else:
1.251 + accessor = ("__load_via_class", accessor, attrname)
1.252 +
1.253 + elif self.first_method == "relative-object":
1.254 + if assigning:
1.255 + emit(("__store_via_object", accessor, attrname, "<assexpr>"))
1.256 + else:
1.257 + accessor = ("__load_via_object", accessor, attrname)
1.258 +
1.259 + elif self.first_method == "relative-object-class":
1.260 + if assigning:
1.261 + emit(("__get_class_and_store", accessor, attrname, "<assexpr>"))
1.262 + else:
1.263 + accessor = ("__get_class_and_load", accessor, attrname)
1.264 +
1.265 + elif self.first_method == "check-class":
1.266 + if assigning:
1.267 + emit(("__check_and_store_via_class", accessor, attrname, "<assexpr>"))
1.268 + else:
1.269 + accessor = ("__check_and_load_via_class", accessor, attrname)
1.270 +
1.271 + elif self.first_method == "check-object":
1.272 + if assigning:
1.273 + emit(("__check_and_store_via_object", accessor, attrname, "<assexpr>"))
1.274 + else:
1.275 + accessor = ("__check_and_load_via_object", accessor, attrname)
1.276 +
1.277 + elif self.first_method == "check-object-class":
1.278 + if assigning:
1.279 + emit(("__check_and_store_via_any", accessor, attrname, "<assexpr>"))
1.280 + else:
1.281 + accessor = ("__check_and_load_via_any", accessor, attrname)
1.282 +
1.283 + return accessor
1.284 +
1.285 + def process_traversed_attributes(self, accessor, context, emit):
1.286 +
1.287 + """
1.288 + Using 'accessor' and 'context', use 'emit' to generate instructions
1.289 + for the traversed attribute accesses. Return the potentially revised
1.290 + accessor and context indicators.
1.291 + """
1.292 +
1.293 + # Traverse attributes using the accessor.
1.294 +
1.295 + num_remaining = len(self.all_subsequent_attributes())
1.296 +
1.297 + if self.traversed_attrnames:
1.298 + for attrname, traversal_mode in self.attribute_traversals():
1.299 + assigning = num_remaining == 1 and self.final_method == "assign"
1.300 +
1.301 + # Set the context, if appropriate.
1.302 +
1.303 + if num_remaining == 1 and self.final_method != "assign" and self.context == "final-accessor":
1.304 +
1.305 + # Invoked attributes employ a separate context accessed
1.306 + # during invocation.
1.307 +
1.308 + if self.final_method in ("access-invoke", "static-invoke"):
1.309 + emit(("<set_context>", accessor))
1.310 + accessor = context = "<context>"
1.311 +
1.312 + # A private context within the access is otherwise
1.313 + # retained.
1.314 +
1.315 + else:
1.316 + emit(("<set_private_context>", accessor))
1.317 + accessor = context = "<private_context>"
1.318 +
1.319 + # Perform the access only if not achieved directly.
1.320 +
1.321 + if num_remaining > 1 or self.final_method in ("access", "access-invoke", "assign"):
1.322 +
1.323 + if traversal_mode == "class":
1.324 + if assigning:
1.325 + emit(("__store_via_class", accessor, attrname, "<assexpr>"))
1.326 + else:
1.327 + accessor = ("__load_via_class", accessor, attrname)
1.328 + else:
1.329 + if assigning:
1.330 + emit(("__store_via_object", accessor, attrname, "<assexpr>"))
1.331 + else:
1.332 + accessor = ("__load_via_object", accessor, attrname)
1.333 +
1.334 + num_remaining -= 1
1.335 +
1.336 + return accessor, context
1.337 +
1.338 + def process_remaining_attributes(self, accessor, context, emit):
1.339 +
1.340 + """
1.341 + Using 'accessor' and 'context', use 'emit' to generate instructions
1.342 + for the remaining attribute accesses. Return the potentially revised
1.343 + accessor and context indicators.
1.344 + """
1.345 +
1.346 + remaining = self.remaining_attrnames
1.347 +
1.348 + if remaining:
1.349 + num_remaining = len(remaining)
1.350 +
1.351 + for attrname in remaining:
1.352 + assigning = num_remaining == 1 and self.final_method == "assign"
1.353 +
1.354 + # Set the context, if appropriate.
1.355 +
1.356 + if num_remaining == 1 and self.final_method != "assign" and self.context == "final-accessor":
1.357 +
1.358 + # Invoked attributes employ a separate context accessed
1.359 + # during invocation.
1.360 +
1.361 + if self.final_method in ("access-invoke", "static-invoke"):
1.362 + emit(("<set_context>", accessor))
1.363 + accessor = context = "<context>"
1.364 +
1.365 + # A private context within the access is otherwise
1.366 + # retained.
1.367 +
1.368 + else:
1.369 + emit(("<set_private_context>", accessor))
1.370 + accessor = context = "<private_context>"
1.371 +
1.372 + # Perform the access only if not achieved directly.
1.373 +
1.374 + if num_remaining > 1 or self.final_method in ("access", "access-invoke", "assign"):
1.375 +
1.376 + # Constrain instructions involving certain special
1.377 + # attribute names.
1.378 +
1.379 + to_search = attrname == "__data__" and "object" or "any"
1.380 +
1.381 + if assigning:
1.382 + emit(("__check_and_store_via_%s" % to_search, accessor, attrname, "<assexpr>"))
1.383 + else:
1.384 + accessor = ("__check_and_load_via_%s" % to_search, accessor, attrname)
1.385 +
1.386 + num_remaining -= 1
1.387 +
1.388 + return accessor, context
1.389 +
1.390 + def process_attribute_access(self, accessor, context, test_accessor, emit):
1.391 +
1.392 + """
1.393 + Using 'accessor','context' and any 'test_accessor' operation, use 'emit'
1.394 + to generate instructions for the final attribute access. Return the
1.395 + potentially revised accessor.
1.396 + """
1.397 +
1.398 + # Define or emit the means of accessing the actual target.
1.399 +
1.400 + if self.final_method in ("static", "static-assign", "static-invoke"):
1.401 +
1.402 + if test_accessor:
1.403 + emit(test_accessor)
1.404 +
1.405 + # Assignments to known attributes.
1.406 +
1.407 + if self.final_method == "static-assign":
1.408 + parent, attrname = self.origin.rsplit(".", 1)
1.409 + emit(("__store_via_object", parent, attrname, "<assexpr>"))
1.410 +
1.411 + # Invoked attributes employ a separate context.
1.412 +
1.413 + elif self.final_method in ("static", "static-invoke"):
1.414 + accessor = ("__load_static_ignore", self.origin)
1.415 +
1.416 + # Wrap accesses in context operations.
1.417 +
1.418 + if self.context_test == "test":
1.419 +
1.420 + # Test and combine the context with static attribute details.
1.421 +
1.422 + if self.final_method == "static":
1.423 + emit(("__load_static_test", context, self.origin))
1.424 +
1.425 + # Test the context, storing it separately if required for the
1.426 + # immediately invoked static attribute.
1.427 +
1.428 + elif self.final_method == "static-invoke":
1.429 + emit(("<test_context_static>", context, self.origin))
1.430 +
1.431 + # Test the context, storing it separately if required for an
1.432 + # immediately invoked attribute.
1.433 +
1.434 + elif self.final_method == "access-invoke":
1.435 + emit(("<test_context_revert>", context, accessor))
1.436 +
1.437 + # Test the context and update the attribute details if
1.438 + # appropriate.
1.439 +
1.440 + else:
1.441 + emit(("__test_context", context, accessor))
1.442 +
1.443 + elif self.context_test == "replace":
1.444 +
1.445 + # Produce an object with updated context.
1.446 +
1.447 + if self.final_method == "static":
1.448 + emit(("__load_static_replace", context, self.origin))
1.449 +
1.450 + # Omit the context update operation where the target is static
1.451 + # and the context is recorded separately.
1.452 +
1.453 + elif self.final_method == "static-invoke":
1.454 + pass
1.455 +
1.456 + # If a separate context is used for an immediate invocation,
1.457 + # produce the attribute details unchanged.
1.458 +
1.459 + elif self.final_method == "access-invoke":
1.460 + emit(accessor)
1.461 +
1.462 + # Update the context in the attribute details.
1.463 +
1.464 + else:
1.465 + emit(("__update_context", context, accessor))
1.466 +
1.467 + # Omit the accessor for assignments and for invocations of static
1.468 + # targets. Otherwise, emit the accessor which may involve the
1.469 + # invocation of a test.
1.470 +
1.471 + elif self.final_method not in ("assign", "static-assign", "static-invoke"):
1.472 + emit(accessor)
1.473 +
1.474 + return accessor
1.475 +
1.476 + def process_context_identity(self, context, emit):
1.477 +
1.478 + """
1.479 + Using 'context', use 'emit' to generate instructions to test the context
1.480 + identity.
1.481 + """
1.482 +
1.483 + if context:
1.484 +
1.485 + # Only verify the context for invocation purposes if a suitable
1.486 + # test has been performed.
1.487 +
1.488 + if self.context_test in ("ignore", "replace") or \
1.489 + self.final_method in ("access-invoke", "static-invoke"):
1.490 +
1.491 + emit(("<context_identity_verified>", context))
1.492 + else:
1.493 + emit(("<context_identity>", context))
1.494 +
1.495 + def write(self, f, location):
1.496 +
1.497 + "Write the plan to file 'f' with the given 'location' information."
1.498 +
1.499 + print >>f, encode_access_location(location), \
1.500 + self.name or "{}", \
1.501 + self.test and "-".join(self.test) or "{}", \
1.502 + self.test_type or "{}", \
1.503 + self.base or "{}", \
1.504 + ".".join(self.traversed_attrnames) or "{}", \
1.505 + ".".join(self.traversed_attrname_modes) or "{}", \
1.506 + ".".join(self.remaining_attrnames) or "{}", \
1.507 + self.context, self.context_test, \
1.508 + self.first_method, self.final_method, self.origin or "{}", \
1.509 + ",".join(self.accessor_kinds)
1.510 +
1.511 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/deducer.py Sun Sep 03 18:21:27 2023 +0200
2.2 +++ b/deducer.py Mon Sep 04 01:23:39 2023 +0200
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Deduce types for usage observations.
2.6
2.7 -Copyright (C) 2014, 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2014-2018, 2023 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -19,6 +19,7 @@
2.13 this program. If not, see <http://www.gnu.org/licenses/>.
2.14 """
2.15
2.16 +from access_plan import AccessPlan
2.17 from common import first, get_assigned_attributes, get_attrnames, \
2.18 get_invoked_attributes, get_name_path, init_item, \
2.19 order_dependencies_partial, sorted_output, \
2.20 @@ -477,23 +478,7 @@
2.21 locations.sort()
2.22
2.23 for location in locations:
2.24 - name, test, test_type, base, \
2.25 - traversed, traversal_modes, attrnames, \
2.26 - context, context_test, \
2.27 - first_method, final_method, \
2.28 - attr, accessor_kinds = self.access_plans[location]
2.29 -
2.30 - print >>f_attrs, encode_access_location(location), \
2.31 - name or "{}", \
2.32 - test and "-".join(test) or "{}", \
2.33 - test_type or "{}", \
2.34 - base or "{}", \
2.35 - ".".join(traversed) or "{}", \
2.36 - ".".join(traversal_modes) or "{}", \
2.37 - ".".join(attrnames) or "{}", \
2.38 - context, context_test, \
2.39 - first_method, final_method, attr or "{}", \
2.40 - ",".join(accessor_kinds)
2.41 + self.access_plans[location].write(f_attrs, location)
2.42
2.43 finally:
2.44 f_attrs.close()
2.45 @@ -2637,333 +2622,18 @@
2.46 (base and "base" or "original-accessor") or \
2.47 "final-accessor"
2.48
2.49 - return name, test, test_type, base, \
2.50 - traversed, traversal_modes, remaining, \
2.51 - context, context_test, \
2.52 - first_method, final_method, \
2.53 - origin, accessor_kinds
2.54 + return AccessPlan(name, test, test_type, base,
2.55 + traversed, traversal_modes, remaining,
2.56 + context, context_test,
2.57 + first_method, final_method,
2.58 + origin, accessor_kinds)
2.59
2.60 def initialise_access_instructions(self):
2.61
2.62 "Expand access plans into instruction sequences."
2.63
2.64 for access_location, access_plan in self.access_plans.items():
2.65 -
2.66 - # Obtain the access details.
2.67 -
2.68 - name, test, test_type, base, \
2.69 - traversed, traversal_modes, attrnames, \
2.70 - context, context_test, \
2.71 - first_method, final_method, \
2.72 - origin, accessor_kinds = access_plan
2.73 -
2.74 - # Emit instructions by appending them to a list.
2.75 -
2.76 - instructions = []
2.77 - emit = instructions.append
2.78 -
2.79 - # Identify any static original accessor.
2.80 -
2.81 - if base:
2.82 - original_accessor = base
2.83 -
2.84 - # Employ names as contexts unless the context needs testing and
2.85 - # potentially updating. In such cases, temporary context storage is
2.86 - # used instead.
2.87 -
2.88 - elif name and not (context_test == "test" and
2.89 - final_method in ("access-invoke", "static-invoke")):
2.90 - original_accessor = "<name>" # refers to the name
2.91 -
2.92 - # Use a generic placeholder representing the access expression in
2.93 - # the general case.
2.94 -
2.95 - else:
2.96 - original_accessor = "<expr>"
2.97 -
2.98 - # Prepare for any first attribute access.
2.99 -
2.100 - if traversed:
2.101 - attrname = traversed[0]
2.102 - del traversed[0]
2.103 - elif attrnames:
2.104 - attrname = attrnames[0]
2.105 - del attrnames[0]
2.106 -
2.107 - # Perform the first access explicitly if at least one operation
2.108 - # requires it.
2.109 -
2.110 - access_first_attribute = final_method in ("access", "access-invoke", "assign") or traversed or attrnames
2.111 -
2.112 - # Determine whether the first access involves assignment.
2.113 -
2.114 - assigning = not traversed and not attrnames and final_method == "assign"
2.115 - set_accessor = assigning and "<set_target_accessor>" or "<set_accessor>"
2.116 - stored_accessor = assigning and "<target_accessor>" or "<accessor>"
2.117 -
2.118 - # Set the context if already available.
2.119 -
2.120 - context_var = None
2.121 -
2.122 - if context == "base":
2.123 - accessor = context_var = (base,)
2.124 - elif context == "original-accessor":
2.125 -
2.126 - # Prevent re-evaluation of any dynamic expression by storing it.
2.127 -
2.128 - if original_accessor == "<expr>":
2.129 - if final_method in ("access-invoke", "static-invoke"):
2.130 - emit(("<set_context>", original_accessor))
2.131 - accessor = context_var = ("<context>",)
2.132 - else:
2.133 - emit((set_accessor, original_accessor))
2.134 - accessor = context_var = (stored_accessor,)
2.135 - else:
2.136 - accessor = context_var = (original_accessor,)
2.137 -
2.138 - # Assigning does not set the context.
2.139 -
2.140 - elif context in ("final-accessor", "unset") and access_first_attribute:
2.141 -
2.142 - # Prevent re-evaluation of any dynamic expression by storing it.
2.143 -
2.144 - if original_accessor == "<expr>":
2.145 - emit((set_accessor, original_accessor))
2.146 - accessor = (stored_accessor,)
2.147 - else:
2.148 - accessor = (original_accessor,)
2.149 - else:
2.150 - accessor = None
2.151 -
2.152 - # Apply any test.
2.153 -
2.154 - if test[0] == "test":
2.155 - test_accessor = accessor = ("__%s_%s_%s" % test, accessor, test_type)
2.156 - else:
2.157 - test_accessor = None
2.158 -
2.159 - # Perform the first or final access.
2.160 - # The access only needs performing if the resulting accessor is used.
2.161 -
2.162 - remaining = len(traversed + attrnames)
2.163 -
2.164 - if access_first_attribute:
2.165 -
2.166 - if first_method == "relative-class":
2.167 - if assigning:
2.168 - emit(("__store_via_class", accessor, attrname, "<assexpr>"))
2.169 - else:
2.170 - accessor = ("__load_via_class", accessor, attrname)
2.171 -
2.172 - elif first_method == "relative-object":
2.173 - if assigning:
2.174 - emit(("__store_via_object", accessor, attrname, "<assexpr>"))
2.175 - else:
2.176 - accessor = ("__load_via_object", accessor, attrname)
2.177 -
2.178 - elif first_method == "relative-object-class":
2.179 - if assigning:
2.180 - emit(("__get_class_and_store", accessor, attrname, "<assexpr>"))
2.181 - else:
2.182 - accessor = ("__get_class_and_load", accessor, attrname)
2.183 -
2.184 - elif first_method == "check-class":
2.185 - if assigning:
2.186 - emit(("__check_and_store_via_class", accessor, attrname, "<assexpr>"))
2.187 - else:
2.188 - accessor = ("__check_and_load_via_class", accessor, attrname)
2.189 -
2.190 - elif first_method == "check-object":
2.191 - if assigning:
2.192 - emit(("__check_and_store_via_object", accessor, attrname, "<assexpr>"))
2.193 - else:
2.194 - accessor = ("__check_and_load_via_object", accessor, attrname)
2.195 -
2.196 - elif first_method == "check-object-class":
2.197 - if assigning:
2.198 - emit(("__check_and_store_via_any", accessor, attrname, "<assexpr>"))
2.199 - else:
2.200 - accessor = ("__check_and_load_via_any", accessor, attrname)
2.201 -
2.202 - # Traverse attributes using the accessor.
2.203 -
2.204 - if traversed:
2.205 - for attrname, traversal_mode in zip(traversed, traversal_modes):
2.206 - assigning = remaining == 1 and final_method == "assign"
2.207 -
2.208 - # Set the context, if appropriate.
2.209 -
2.210 - if remaining == 1 and final_method != "assign" and context == "final-accessor":
2.211 -
2.212 - # Invoked attributes employ a separate context accessed
2.213 - # during invocation.
2.214 -
2.215 - if final_method in ("access-invoke", "static-invoke"):
2.216 - emit(("<set_context>", accessor))
2.217 - accessor = context_var = "<context>"
2.218 -
2.219 - # A private context within the access is otherwise
2.220 - # retained.
2.221 -
2.222 - else:
2.223 - emit(("<set_private_context>", accessor))
2.224 - accessor = context_var = "<private_context>"
2.225 -
2.226 - # Perform the access only if not achieved directly.
2.227 -
2.228 - if remaining > 1 or final_method in ("access", "access-invoke", "assign"):
2.229 -
2.230 - if traversal_mode == "class":
2.231 - if assigning:
2.232 - emit(("__store_via_class", accessor, attrname, "<assexpr>"))
2.233 - else:
2.234 - accessor = ("__load_via_class", accessor, attrname)
2.235 - else:
2.236 - if assigning:
2.237 - emit(("__store_via_object", accessor, attrname, "<assexpr>"))
2.238 - else:
2.239 - accessor = ("__load_via_object", accessor, attrname)
2.240 -
2.241 - remaining -= 1
2.242 -
2.243 - if attrnames:
2.244 - for attrname in attrnames:
2.245 - assigning = remaining == 1 and final_method == "assign"
2.246 -
2.247 - # Set the context, if appropriate.
2.248 -
2.249 - if remaining == 1 and final_method != "assign" and context == "final-accessor":
2.250 -
2.251 - # Invoked attributes employ a separate context accessed
2.252 - # during invocation.
2.253 -
2.254 - if final_method in ("access-invoke", "static-invoke"):
2.255 - emit(("<set_context>", accessor))
2.256 - accessor = context_var = "<context>"
2.257 -
2.258 - # A private context within the access is otherwise
2.259 - # retained.
2.260 -
2.261 - else:
2.262 - emit(("<set_private_context>", accessor))
2.263 - accessor = context_var = "<private_context>"
2.264 -
2.265 - # Perform the access only if not achieved directly.
2.266 -
2.267 - if remaining > 1 or final_method in ("access", "access-invoke", "assign"):
2.268 -
2.269 - # Constrain instructions involving certain special
2.270 - # attribute names.
2.271 -
2.272 - to_search = attrname == "__data__" and "object" or "any"
2.273 -
2.274 - if assigning:
2.275 - emit(("__check_and_store_via_%s" % to_search, accessor, attrname, "<assexpr>"))
2.276 - else:
2.277 - accessor = ("__check_and_load_via_%s" % to_search, accessor, attrname)
2.278 -
2.279 - remaining -= 1
2.280 -
2.281 - # Make any accessor test available if not emitted.
2.282 -
2.283 - test_accessor = not instructions and test_accessor or None
2.284 -
2.285 - # Define or emit the means of accessing the actual target.
2.286 -
2.287 - if final_method in ("static", "static-assign", "static-invoke"):
2.288 -
2.289 - if test_accessor:
2.290 - emit(test_accessor)
2.291 -
2.292 - # Assignments to known attributes.
2.293 -
2.294 - if final_method == "static-assign":
2.295 - parent, attrname = origin.rsplit(".", 1)
2.296 - emit(("__store_via_object", parent, attrname, "<assexpr>"))
2.297 -
2.298 - # Invoked attributes employ a separate context.
2.299 -
2.300 - elif final_method in ("static", "static-invoke"):
2.301 - accessor = ("__load_static_ignore", origin)
2.302 -
2.303 - # Wrap accesses in context operations.
2.304 -
2.305 - if context_test == "test":
2.306 -
2.307 - # Test and combine the context with static attribute details.
2.308 -
2.309 - if final_method == "static":
2.310 - emit(("__load_static_test", context_var, origin))
2.311 -
2.312 - # Test the context, storing it separately if required for the
2.313 - # immediately invoked static attribute.
2.314 -
2.315 - elif final_method == "static-invoke":
2.316 - emit(("<test_context_static>", context_var, origin))
2.317 -
2.318 - # Test the context, storing it separately if required for an
2.319 - # immediately invoked attribute.
2.320 -
2.321 - elif final_method == "access-invoke":
2.322 - emit(("<test_context_revert>", context_var, accessor))
2.323 -
2.324 - # Test the context and update the attribute details if
2.325 - # appropriate.
2.326 -
2.327 - else:
2.328 - emit(("__test_context", context_var, accessor))
2.329 -
2.330 - elif context_test == "replace":
2.331 -
2.332 - # Produce an object with updated context.
2.333 -
2.334 - if final_method == "static":
2.335 - emit(("__load_static_replace", context_var, origin))
2.336 -
2.337 - # Omit the context update operation where the target is static
2.338 - # and the context is recorded separately.
2.339 -
2.340 - elif final_method == "static-invoke":
2.341 - pass
2.342 -
2.343 - # If a separate context is used for an immediate invocation,
2.344 - # produce the attribute details unchanged.
2.345 -
2.346 - elif final_method == "access-invoke":
2.347 - emit(accessor)
2.348 -
2.349 - # Update the context in the attribute details.
2.350 -
2.351 - else:
2.352 - emit(("__update_context", context_var, accessor))
2.353 -
2.354 - # Omit the accessor for assignments and for invocations of static
2.355 - # targets. Otherwise, emit the accessor which may involve the
2.356 - # invocation of a test.
2.357 -
2.358 - elif final_method not in ("assign", "static-assign", "static-invoke"):
2.359 - emit(accessor)
2.360 -
2.361 - # Produce an advisory instruction regarding the context.
2.362 -
2.363 - if context_var:
2.364 -
2.365 - # Only verify the context for invocation purposes if a suitable
2.366 - # test has been performed.
2.367 -
2.368 - if context_test in ("ignore", "replace") or \
2.369 - final_method in ("access-invoke", "static-invoke"):
2.370 -
2.371 - emit(("<context_identity_verified>", context_var))
2.372 - else:
2.373 - emit(("<context_identity>", context_var))
2.374 -
2.375 - # Produce an advisory instruction regarding the final attribute.
2.376 -
2.377 - if origin:
2.378 - emit(("<final_identity>", origin))
2.379 -
2.380 - self.access_instructions[access_location] = instructions
2.381 - self.accessor_kinds[access_location] = accessor_kinds
2.382 + self.access_instructions[access_location] = access_plan.get_instructions()
2.383 + self.accessor_kinds[access_location] = access_plan.accessor_kinds
2.384
2.385 # vim: tabstop=4 expandtab shiftwidth=4