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
2.1 --- a/deducer.py Sun Sep 03 00:24:29 2023 +0200
2.2 +++ b/deducer.py Tue Sep 05 21:16:49 2023 +0200
2.3 @@ -19,6 +19,7 @@
2.4 this program. If not, see <http://www.gnu.org/licenses/>.
2.5 """
2.6
2.7 +from access_plan import AccessPlan
2.8 from common import first, get_assigned_attributes, get_attrnames, \
2.9 get_invoked_attributes, get_name_path, init_item, \
2.10 order_dependencies_partial, sorted_output, \
2.11 @@ -477,23 +478,7 @@
2.12 locations.sort()
2.13
2.14 for location in locations:
2.15 - name, test, test_type, base, \
2.16 - traversed, traversal_modes, attrnames, \
2.17 - context, context_test, \
2.18 - first_method, final_method, \
2.19 - attr, accessor_kinds = self.access_plans[location]
2.20 -
2.21 - print >>f_attrs, encode_access_location(location), \
2.22 - name or "{}", \
2.23 - test and "-".join(test) or "{}", \
2.24 - test_type or "{}", \
2.25 - base or "{}", \
2.26 - ".".join(traversed) or "{}", \
2.27 - ".".join(traversal_modes) or "{}", \
2.28 - ".".join(attrnames) or "{}", \
2.29 - context, context_test, \
2.30 - first_method, final_method, attr or "{}", \
2.31 - ",".join(accessor_kinds)
2.32 + self.access_plans[location].write(f_attrs, location)
2.33
2.34 finally:
2.35 f_attrs.close()
2.36 @@ -2637,364 +2622,18 @@
2.37 (base and "base" or "original-accessor") or \
2.38 "final-accessor"
2.39
2.40 - return name, test, test_type, base, \
2.41 - traversed, traversal_modes, remaining, \
2.42 - context, context_test, \
2.43 - first_method, final_method, \
2.44 - origin, accessor_kinds
2.45 + return AccessPlan(name, test, test_type, base,
2.46 + traversed, traversal_modes, remaining,
2.47 + context, context_test,
2.48 + first_method, final_method,
2.49 + origin, accessor_kinds)
2.50
2.51 def initialise_access_instructions(self):
2.52
2.53 "Expand access plans into instruction sequences."
2.54
2.55 for access_location, access_plan in self.access_plans.items():
2.56 -
2.57 - # Obtain the access details.
2.58 -
2.59 - name, test, test_type, base, \
2.60 - traversed, traversal_modes, attrnames, \
2.61 - context, context_test, \
2.62 - first_method, final_method, \
2.63 - origin, accessor_kinds = access_plan
2.64 -
2.65 - # Emit instructions by appending them to a list.
2.66 -
2.67 - instructions = []
2.68 - emit = instructions.append
2.69 -
2.70 - # Identify any static original accessor.
2.71 -
2.72 - if base:
2.73 - original_accessor = base
2.74 -
2.75 - # Employ names as contexts unless the context needs testing and
2.76 - # potentially updating. In such cases, temporary context storage is
2.77 - # used instead.
2.78 -
2.79 - elif name and not (context_test == "test" and
2.80 - final_method in ("access-invoke", "static-invoke")):
2.81 - original_accessor = "<name>" # refers to the name
2.82 -
2.83 - # Use a generic placeholder representing the access expression in
2.84 - # the general case.
2.85 -
2.86 - else:
2.87 - original_accessor = "<expr>"
2.88 -
2.89 - # Prepare for any first attribute access.
2.90 -
2.91 - if traversed:
2.92 - attrname = traversed[0]
2.93 - del traversed[0]
2.94 - elif attrnames:
2.95 - attrname = attrnames[0]
2.96 - del attrnames[0]
2.97 -
2.98 - # Perform the first access explicitly if at least one operation
2.99 - # requires it.
2.100 -
2.101 - access_first_attribute = final_method in ("access", "access-invoke", "assign") or traversed or attrnames
2.102 -
2.103 - # Determine whether the first access involves assignment.
2.104 -
2.105 - assigning = not traversed and not attrnames and final_method == "assign"
2.106 - set_accessor = assigning and "<set_target_accessor>" or "<set_accessor>"
2.107 - stored_accessor = assigning and "<target_accessor>" or "<accessor>"
2.108 -
2.109 - # Set the context if already available.
2.110 -
2.111 - context_var = None
2.112 -
2.113 - if context == "base":
2.114 - accessor = context_var = (base,)
2.115 - elif context == "original-accessor":
2.116 -
2.117 - # Prevent re-evaluation of any dynamic expression by storing it.
2.118 -
2.119 - if original_accessor == "<expr>":
2.120 - if final_method in ("access-invoke", "static-invoke"):
2.121 - emit(("<set_context>", original_accessor))
2.122 - accessor = context_var = ("<context>",)
2.123 - else:
2.124 - emit((set_accessor, original_accessor))
2.125 - accessor = context_var = (stored_accessor,)
2.126 - else:
2.127 - accessor = context_var = (original_accessor,)
2.128 -
2.129 - # Assigning does not set the context.
2.130 -
2.131 - elif context in ("final-accessor", "unset") and access_first_attribute:
2.132 -
2.133 - # Prevent re-evaluation of any dynamic expression by storing it.
2.134 -
2.135 - if original_accessor == "<expr>":
2.136 - emit((set_accessor, original_accessor))
2.137 - accessor = (stored_accessor,)
2.138 - else:
2.139 - accessor = (original_accessor,)
2.140 - else:
2.141 - accessor = None
2.142 -
2.143 - # Apply any test.
2.144 -
2.145 - if test[0] == "test":
2.146 - test_accessor = accessor = ("__%s_%s_%s" % test, accessor, test_type)
2.147 - else:
2.148 - test_accessor = None
2.149 -
2.150 - # Perform the first or final access.
2.151 - # The access only needs performing if the resulting accessor is used.
2.152 -
2.153 - remaining = len(traversed + attrnames)
2.154 -
2.155 - if access_first_attribute:
2.156 -
2.157 - # Access via the accessor's class.
2.158 -
2.159 - if first_method == "relative-class":
2.160 - if assigning:
2.161 - emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
2.162 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.163 - else:
2.164 - accessor = ("__load_via_class", accessor, attrname)
2.165 -
2.166 - # Access via the accessor itself.
2.167 -
2.168 - elif first_method == "relative-object":
2.169 - if assigning:
2.170 - emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
2.171 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.172 - else:
2.173 - accessor = ("__load_via_object", accessor, attrname)
2.174 -
2.175 - # Access via a class accessor or the accessor's class.
2.176 -
2.177 - elif first_method == "relative-object-class":
2.178 - if assigning:
2.179 - emit(("__raise_type_error",))
2.180 - else:
2.181 - accessor = ("__get_class_and_load", accessor, attrname)
2.182 -
2.183 - # Access via the accessor's class.
2.184 -
2.185 - elif first_method == "check-class":
2.186 - if assigning:
2.187 - emit(("__raise_type_error",))
2.188 - else:
2.189 - accessor = ("__check_and_load_via_class", accessor, attrname)
2.190 -
2.191 - # Access via the accessor itself.
2.192 -
2.193 - elif first_method == "check-object":
2.194 - if assigning:
2.195 - emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
2.196 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.197 - else:
2.198 - accessor = ("__check_and_load_via_object", accessor, attrname)
2.199 -
2.200 - # Access via a class accessor or the accessor's class.
2.201 - # Here, only access via the accessor is supported.
2.202 -
2.203 - elif first_method == "check-object-class":
2.204 - if assigning:
2.205 - emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
2.206 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.207 - else:
2.208 - accessor = ("__check_and_load_via_any", accessor, attrname)
2.209 -
2.210 - # Traverse attributes using the accessor.
2.211 -
2.212 - if traversed:
2.213 - for attrname, traversal_mode in zip(traversed, traversal_modes):
2.214 - assigning = remaining == 1 and final_method == "assign"
2.215 -
2.216 - # Set the context, if appropriate.
2.217 -
2.218 - if remaining == 1 and final_method != "assign" and context == "final-accessor":
2.219 -
2.220 - # Invoked attributes employ a separate context accessed
2.221 - # during invocation.
2.222 -
2.223 - if final_method in ("access-invoke", "static-invoke"):
2.224 - emit(("<set_context>", accessor))
2.225 - accessor = context_var = "<context>"
2.226 -
2.227 - # A private context within the access is otherwise
2.228 - # retained.
2.229 -
2.230 - else:
2.231 - emit(("<set_private_context>", accessor))
2.232 - accessor = context_var = "<private_context>"
2.233 -
2.234 - # Perform the access only if not achieved directly.
2.235 -
2.236 - if remaining > 1 or final_method in ("access", "access-invoke", "assign"):
2.237 -
2.238 - if traversal_mode == "class":
2.239 - if assigning:
2.240 - emit(("<set_attr_ref>", ("__get_class_attr_ref", accessor, attrname)))
2.241 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.242 - else:
2.243 - accessor = ("__load_via_class", accessor, attrname)
2.244 - else:
2.245 - if assigning:
2.246 - emit(("<set_attr_ref>", ("__get_object_attr_ref", accessor, attrname)))
2.247 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.248 - else:
2.249 - accessor = ("__load_via_object", accessor, attrname)
2.250 -
2.251 - remaining -= 1
2.252 -
2.253 - if attrnames:
2.254 - for attrname in attrnames:
2.255 - assigning = remaining == 1 and final_method == "assign"
2.256 -
2.257 - # Set the context, if appropriate.
2.258 -
2.259 - if remaining == 1 and final_method != "assign" and context == "final-accessor":
2.260 -
2.261 - # Invoked attributes employ a separate context accessed
2.262 - # during invocation.
2.263 -
2.264 - if final_method in ("access-invoke", "static-invoke"):
2.265 - emit(("<set_context>", accessor))
2.266 - accessor = context_var = "<context>"
2.267 -
2.268 - # A private context within the access is otherwise
2.269 - # retained.
2.270 -
2.271 - else:
2.272 - emit(("<set_private_context>", accessor))
2.273 - accessor = context_var = "<private_context>"
2.274 -
2.275 - # Perform the access only if not achieved directly.
2.276 -
2.277 - if remaining > 1 or final_method in ("access", "access-invoke", "assign"):
2.278 -
2.279 - # Constrain instructions involving certain special
2.280 - # attribute names.
2.281 -
2.282 - to_search = attrname != "__data__" and "any" or "object"
2.283 -
2.284 - if assigning:
2.285 - emit(("<set_attr_ref>", ("__check_and_get_object_attr_ref", accessor, attrname)))
2.286 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.287 - else:
2.288 - accessor = ("__check_and_load_via_%s" % to_search, accessor, attrname)
2.289 -
2.290 - remaining -= 1
2.291 -
2.292 - # Make any accessor test available if not emitted.
2.293 -
2.294 - test_accessor = not instructions and test_accessor or None
2.295 -
2.296 - # Define or emit the means of accessing the actual target.
2.297 -
2.298 - if final_method in ("static", "static-assign", "static-invoke"):
2.299 -
2.300 - if test_accessor:
2.301 - emit(test_accessor)
2.302 -
2.303 - # Assignments to known attributes.
2.304 -
2.305 - if final_method == "static-assign":
2.306 - parent, attrname = origin.rsplit(".", 1)
2.307 - emit(("<set_attr_ref>", ("__get_object_attr_ref", parent, attrname)))
2.308 - emit(("__store_via_attr_ref", "<attr_ref>", "<assexpr>"))
2.309 -
2.310 - # Invoked attributes employ a separate context.
2.311 -
2.312 - elif final_method in ("static", "static-invoke"):
2.313 - accessor = ("__load_static_ignore", origin)
2.314 -
2.315 - # Define any replacement context variable plus the default eventual
2.316 - # context variable.
2.317 -
2.318 - replacement_context_var = "<new_context>"
2.319 - final_context_var = context_var
2.320 -
2.321 - # Wrap accesses in context operations.
2.322 -
2.323 - if context_test == "test":
2.324 -
2.325 - # Test and combine the context with static attribute details.
2.326 -
2.327 - if final_method == "static":
2.328 - emit(("__load_static_test", replacement_context_var, context_var, origin))
2.329 - final_context_var = replacement_context_var
2.330 -
2.331 - # Test the context, storing it separately if required for the
2.332 - # immediately invoked static attribute.
2.333 -
2.334 - elif final_method == "static-invoke":
2.335 - emit(("<test_context_static>", context_var, origin))
2.336 -
2.337 - # Test the context, storing it separately if required for an
2.338 - # immediately invoked attribute.
2.339 -
2.340 - elif final_method == "access-invoke":
2.341 - emit(("<test_context_revert>", context_var, accessor))
2.342 -
2.343 - # Test the context and update the attribute details if
2.344 - # appropriate.
2.345 -
2.346 - else:
2.347 - emit(("__test_context", replacement_context_var, context_var, accessor))
2.348 - final_context_var = replacement_context_var
2.349 -
2.350 - elif context_test == "replace":
2.351 -
2.352 - # Produce an object with updated context.
2.353 -
2.354 - if final_method == "static":
2.355 - emit(("__load_static_replace", replacement_context_var, context_var, origin))
2.356 - final_context_var = replacement_context_var
2.357 -
2.358 - # Omit the context update operation where the target is static
2.359 - # and the context is recorded separately.
2.360 -
2.361 - elif final_method == "static-invoke":
2.362 - pass
2.363 -
2.364 - # If a separate context is used for an immediate invocation,
2.365 - # produce the attribute details unchanged.
2.366 -
2.367 - elif final_method == "access-invoke":
2.368 - emit(accessor)
2.369 -
2.370 - # Update the context in the attribute details.
2.371 -
2.372 - else:
2.373 - emit(("__update_context", replacement_context_var, context_var, accessor))
2.374 - final_context_var = replacement_context_var
2.375 -
2.376 - # Omit the accessor for assignments and for invocations of static
2.377 - # targets. Otherwise, emit the accessor which may involve the
2.378 - # invocation of a test.
2.379 -
2.380 - elif final_method not in ("assign", "static-assign", "static-invoke"):
2.381 - emit(accessor)
2.382 -
2.383 - # Produce an advisory instruction regarding the context.
2.384 -
2.385 - if final_context_var:
2.386 -
2.387 - # Only verify the context for invocation purposes if a suitable
2.388 - # test has been performed.
2.389 -
2.390 - if context_test in ("ignore", "replace") or \
2.391 - final_method in ("access-invoke", "static-invoke"):
2.392 -
2.393 - emit(("<context_identity_verified>", final_context_var))
2.394 - else:
2.395 - emit(("<context_identity>", final_context_var))
2.396 -
2.397 - # Produce an advisory instruction regarding the final attribute.
2.398 -
2.399 - if origin:
2.400 - emit(("<final_identity>", origin))
2.401 -
2.402 - self.access_instructions[access_location] = instructions
2.403 - self.accessor_kinds[access_location] = accessor_kinds
2.404 + self.access_instructions[access_location] = access_plan.get_instructions()
2.405 + self.accessor_kinds[access_location] = access_plan.accessor_kinds
2.406
2.407 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/docs/wiki/Deduction Sun Sep 03 00:24:29 2023 +0200
3.2 +++ b/docs/wiki/Deduction Tue Sep 05 21:16:49 2023 +0200
3.3 @@ -89,7 +89,7 @@
3.4 //format=svg
3.5 //transform=notugly
3.6 digraph indexes {
3.7 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Indexes"];
3.8 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Indexes"];
3.9 edge [tooltip="Indexes"];
3.10 rankdir=LR;
3.11
3.12 @@ -142,7 +142,7 @@
3.13 //format=svg
3.14 //transform=notugly
3.15 digraph deduction {
3.16 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Deduction"];
3.17 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Deduction"];
3.18 edge [tooltip="Deduction"];
3.19 rankdir=LR;
3.20
3.21 @@ -192,7 +192,7 @@
3.22 //format=svg
3.23 //transform=notugly
3.24 digraph usage_to_types {
3.25 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Usage to types"];
3.26 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Usage to types"];
3.27 edge [tooltip="Usage to types"];
3.28 rankdir=LR;
3.29
3.30 @@ -249,7 +249,7 @@
3.31 //format=svg
3.32 //transform=notugly
3.33 digraph instance_providers {
3.34 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Instance providers"];
3.35 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Instance providers"];
3.36 edge [tooltip="Instance providers"];
3.37 rankdir=LR;
3.38
3.39 @@ -310,7 +310,7 @@
3.40 //format=svg
3.41 //transform=notugly
3.42 digraph assignments {
3.43 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Attribute assignments"];
3.44 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Attribute assignments"];
3.45 edge [tooltip="Attribute assignments"];
3.46 rankdir=LR;
3.47
3.48 @@ -736,19 +736,34 @@
3.49 .. position
3.50 }}}
3.51
3.52 -==== Direct Store ====
3.53 +==== Attribute Reference ====
3.54
3.55 -These instructions employ the attribute position for the supplied attribute
3.56 -name, storing an attribute value.
3.57 +Attribute references are obtained to be targets for store operations as well
3.58 +as being exposed as result targets.
3.59
3.60 {{{#!table
3.61 '''Instruction''' || '''Arguments''' || '''Operations'''
3.62 ==
3.63 -`__store_via_class` || object, attribute name, value
3.64 -|| Obtain class from object; store attribute in class at position
3.65 +`__get_class_attr_ref` || object, attribute name
3.66 +|| Obtain class from object; obtain reference for attribute in class
3.67 ==
3.68 -`__store_via_object` || object, attribute name, value
3.69 -|| Store attribute in object at position
3.70 +`__get_object_attr_ref` || object, attribute name
3.71 +|| Obtain reference for attribute in object
3.72 +}}}
3.73 +
3.74 +==== Attribute Reference Load and Store ====
3.75 +
3.76 +These instructions employ an attribute reference, previously obtained for a
3.77 +given attribute in an object.
3.78 +
3.79 +{{{#!table
3.80 +'''Instruction''' || '''Arguments''' || '''Operations'''
3.81 +==
3.82 +`__load_via_attr_ref` || attribute reference
3.83 +|| Load value from attribute reference
3.84 +==
3.85 +`__store_via_attr_ref` || attribute reference, value
3.86 +|| Store value in attribute at reference
3.87 }}}
3.88
3.89 ==== Checked Load ====
3.90 @@ -770,7 +785,7 @@
3.91 .. raise type error
3.92 }}}
3.93
3.94 -==== Checked Store ====
3.95 +==== Checked Attribute Reference ====
3.96
3.97 These instructions employ the attribute position and code for the supplied
3.98 attribute name, storing an attribute value.
3.99 @@ -778,14 +793,8 @@
3.100 {{{#!table
3.101 '''Instruction''' || '''Arguments''' || '''Operations'''
3.102 ==
3.103 -`__check_and_store_via_class` || object, attribute name, value
3.104 -|| Raise type error
3.105 -==
3.106 -`__check_and_store_via_object` || object, attribute name, value
3.107 -|| Test for attribute and store value or raise type error
3.108 -==
3.109 -`__check_and_store_via_any` || object, attribute name, value
3.110 -|| Test for attribute and store value or raise type error
3.111 +`__check_and_get_object_attr_ref` || object, attribute name
3.112 +|| Test for attribute and obtain reference or raise type error
3.113 }}}
3.114
3.115 ==== Testing ====
3.116 @@ -844,20 +853,29 @@
3.117 {{{#!table
3.118 '''Instruction''' || '''Arguments''' || '''Operations'''
3.119 ==
3.120 -`__get_context` || (temporary)
3.121 -|| Load the context stored in the temporary storage
3.122 +`__get_accessor` || (temporary)
3.123 +|| Load the indicated accessor from temporary storage
3.124 +==
3.125 +`__get_attr_ref` || (temporary)
3.126 +|| Load the indicated attribute reference from temporary storage
3.127 ==
3.128 -`__set_accessor` || accessor
3.129 -|| Store the accessor temporarily
3.130 +`__get_context` || (temporary)
3.131 +|| Load the indicated context from temporary storage
3.132 +==
3.133 +`__set_accessor` || (temporary), accessor
3.134 +|| Store the accessor in temporary storage
3.135 +==
3.136 +`__set_attr_ref` || (temporary), reference
3.137 +|| Store the given attribute reference in temporary storage
3.138 ==
3.139 `__set_context` || (temporary), context
3.140 -|| Store the context in the temporary storage
3.141 +|| Store the given context in temporary storage
3.142 ==
3.143 `__set_private_context` || context
3.144 -|| Store the context temporarily
3.145 +|| Store the context in temporary storage
3.146 ==
3.147 `__set_target_accessor` || accessor
3.148 -|| Store the assignment accessor temporarily
3.149 +|| Store the assignment accessor in temporary storage
3.150 }}}
3.151
3.152 ==== Context Test ====
4.1 --- a/docs/wiki/Imports Sun Sep 03 00:24:29 2023 +0200
4.2 +++ b/docs/wiki/Imports Tue Sep 05 21:16:49 2023 +0200
4.3 @@ -95,7 +95,7 @@
4.4 //format=svg
4.5 //transform=notugly
4.6 digraph mutual {
4.7 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Mutually-dependent modules"];
4.8 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Mutually-dependent modules"];
4.9 edge [tooltip="Mutually-dependent modules"];
4.10 rankdir=LR;
4.11
4.12 @@ -209,7 +209,7 @@
4.13 //format=svg
4.14 //transform=notugly
4.15 digraph imports {
4.16 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Import dependencies"];
4.17 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Import dependencies"];
4.18 edge [tooltip="Import dependencies"];
4.19 rankdir=LR;
4.20
5.1 --- a/docs/wiki/Inspection Sun Sep 03 00:24:29 2023 +0200
5.2 +++ b/docs/wiki/Inspection Tue Sep 05 21:16:49 2023 +0200
5.3 @@ -179,7 +179,7 @@
5.4 //format=svg
5.5 //transform=notugly
5.6 digraph accesses {
5.7 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Names and accesses"];
5.8 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Names and accesses"];
5.9 edge [tooltip="Names and accesses"];
5.10 rankdir=TB;
5.11
5.12 @@ -316,7 +316,7 @@
5.13 //format=svg
5.14 //transform=notugly
5.15 digraph usage {
5.16 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Name and attribute tracking"];
5.17 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Name and attribute tracking"];
5.18 edge [tooltip="Name and attribute tracking"];
5.19 rankdir=TB;
5.20
6.1 --- a/docs/wiki/Representations Sun Sep 03 00:24:29 2023 +0200
6.2 +++ b/docs/wiki/Representations Tue Sep 05 21:16:49 2023 +0200
6.3 @@ -16,8 +16,8 @@
6.4 //format=svg
6.5 //transform=notugly
6.6 digraph attributes {
6.7 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Attributes"];
6.8 - edge [fontsize="13.0",fontname="sans-serif",tooltip="Attributes"];
6.9 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Attributes"];
6.10 + edge [fontsize="12.0",fontname="sans-serif",tooltip="Attributes"];
6.11 rankdir=TB;
6.12
6.13 attrA [label="attribute | { value |<value> reference to object }",shape=record];
6.14 @@ -77,8 +77,8 @@
6.15 //format=svg
6.16 //transform=notugly
6.17 digraph wrappers {
6.18 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Wrappers"];
6.19 - edge [fontsize="13.0",fontname="sans-serif",tooltip="Wrappers"];
6.20 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Wrappers"];
6.21 + edge [fontsize="12.0",fontname="sans-serif",tooltip="Wrappers"];
6.22 rankdir=TB;
6.23
6.24 inst [label="<main> instance | { attr1 |<attr1> reference to method } | { attr2 | value } | ...",shape=record];
6.25 @@ -104,8 +104,8 @@
6.26 //format=svg
6.27 //transform=notugly
6.28 digraph objects {
6.29 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Objects"];
6.30 - edge [fontsize="13.0",fontname="sans-serif",tooltip="Objects"];
6.31 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Objects"];
6.32 + edge [fontsize="12.0",fontname="sans-serif",tooltip="Objects"];
6.33 rankdir=TB;
6.34
6.35 instC [label="<main> instance of C | { 0 | reference to\ninstance table } | { __class__ |<cls> reference\nto C } | { a | value } | { b | value } | ...",shape=record];
7.1 --- a/docs/wiki/Restarted Sun Sep 03 00:24:29 2023 +0200
7.2 +++ b/docs/wiki/Restarted Tue Sep 05 21:16:49 2023 +0200
7.3 @@ -262,8 +262,8 @@
7.4 //format=svg
7.5 //transform=notugly
7.6 digraph structures {
7.7 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Instance and class structures"];
7.8 - edge [fontsize="13.0",fontname="sans-serif",tooltip="Instance and class structures"];
7.9 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Instance and class structures"];
7.10 + edge [fontsize="12.0",fontname="sans-serif",tooltip="Instance and class structures"];
7.11 rankdir=TB;
7.12
7.13 instanceC [label="<main> instance of C |{ context of a | value of a }|{context of b | value of b }",shape=record];
7.14 @@ -285,8 +285,8 @@
7.15 //format=svg
7.16 //transform=notugly
7.17 digraph methods {
7.18 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Method structures"];
7.19 - edge [fontsize="13.0",fontname="sans-serif",tooltip="Method structures"];
7.20 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Method structures"];
7.21 + edge [fontsize="12.0",fontname="sans-serif",tooltip="Method structures"];
7.22 rankdir=TB;
7.23
7.24 classC [label="<main> class C | { context of m | <mvalue> uncallable for m } | ...",shape=record];
8.1 --- a/docs/wiki/Structure Sun Sep 03 00:24:29 2023 +0200
8.2 +++ b/docs/wiki/Structure Tue Sep 05 21:16:49 2023 +0200
8.3 @@ -29,7 +29,7 @@
8.4 //format=svg
8.5 //transform=notugly
8.6 digraph program {
8.7 - node [shape=box,fontsize="13.0",fontname="sans-serif",tooltip="Program structure"];
8.8 + node [shape=box,fontsize="12.0",fontname="sans-serif",tooltip="Program structure"];
8.9 edge [tooltip="Program structure"];
8.10 rankdir=LR;
8.11
9.1 --- a/templates/native/common.c Sun Sep 03 00:24:29 2023 +0200
9.2 +++ b/templates/native/common.c Tue Sep 05 21:16:49 2023 +0200
9.3 @@ -30,9 +30,9 @@
9.4 {
9.5 /* Create a new string and mutate the __data__, __size__ and __key__ attributes. */
9.6 __attr attr = __NEWINSTANCE(__builtins___str_str);
9.7 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(attr), __data__), (__attr) {.strvalue=s});
9.8 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(attr), __size__), __INTVALUE(size));
9.9 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(attr), __key__), __NULL);
9.10 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(attr), __data__), (__attr) {.strvalue=s});
9.11 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(attr), __size__), __INTVALUE(size));
9.12 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(attr), __key__), __NULL);
9.13 return attr;
9.14 }
9.15
9.16 @@ -40,7 +40,7 @@
9.17 {
9.18 /* Create a new list and mutate the __data__ attribute. */
9.19 __attr attr = __NEWINSTANCE(__builtins___list_list);
9.20 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(attr), __data__), (__attr) {.seqvalue=f});
9.21 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(attr), __data__), (__attr) {.seqvalue=f});
9.22 return attr;
9.23 }
9.24
10.1 --- a/templates/native/list.c Sun Sep 03 00:24:29 2023 +0200
10.2 +++ b/templates/native/list.c Tue Sep 05 21:16:49 2023 +0200
10.3 @@ -56,7 +56,7 @@
10.4
10.5 /* Replace the __data__ attribute if appropriate. */
10.6 if (newdata != data)
10.7 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(self), __data__), ((__attr) {.seqvalue=newdata}));
10.8 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(self), __data__), ((__attr) {.seqvalue=newdata}));
10.9 return __builtins___none_None;
10.10 }
10.11
10.12 @@ -84,7 +84,7 @@
10.13
10.14 /* Replace the __data__ attribute if appropriate. */
10.15 if (newdata != data)
10.16 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(self), __data__), ((__attr) {.seqvalue=newdata}));
10.17 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(self), __data__), ((__attr) {.seqvalue=newdata}));
10.18 return __builtins___none_None;
10.19 }
10.20
11.1 --- a/templates/ops.c Sun Sep 03 00:24:29 2023 +0200
11.2 +++ b/templates/ops.c Tue Sep 05 21:16:49 2023 +0200
11.3 @@ -417,7 +417,7 @@
11.4
11.5 void *__ALLOCATEIM(size_t nmemb, size_t size)
11.6 {
11.7 - void *ptr = GC_MALLOC_ATOMIC(nmemb * size); /* sets memory to zero */
11.8 + void *ptr = GC_MALLOC_ATOMIC(nmemb * size);
11.9 if (ptr == NULL)
11.10 __raise_memory_error();
11.11 return ptr;
12.1 --- a/templates/ops.h Sun Sep 03 00:24:29 2023 +0200
12.2 +++ b/templates/ops.h Tue Sep 05 21:16:49 2023 +0200
12.3 @@ -46,6 +46,8 @@
12.4 #define __load_via_attr_ref(ATTR) (*(ATTR))
12.5 #define __store_via_attr_ref(ATTR, VALUE) __set_attr(ATTR, VALUE)
12.6
12.7 +#define __store_via_attr_ref__(ATTR, VALUE) (*(ATTR) = VALUE)
12.8 +
12.9 /* Direct retrieval operations, returning attributes. */
12.10
12.11 __attr __load_via_class__(__ref obj, int pos);
13.1 --- a/templates/progops.c Sun Sep 03 00:24:29 2023 +0200
13.2 +++ b/templates/progops.c Tue Sep 05 21:16:49 2023 +0200
13.3 @@ -32,7 +32,7 @@
13.4 {
13.5 obj->table = table;
13.6 obj->pos = __INSTANCEPOS;
13.7 - __store_via_attr_ref(__get_object_attr_ref(obj, __class__), __ATTRVALUE(cls));
13.8 + __store_via_attr_ref__(__get_object_attr_ref(obj, __class__), __ATTRVALUE(cls));
13.9 }
13.10
13.11 __attr __new(const __table * table, __ref cls, size_t size, int immutable)
13.12 @@ -81,7 +81,7 @@
13.13
13.14 /* Store a reference to the data in the object's __data__ attribute. */
13.15
13.16 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(self), __data__), (__attr) {.seqvalue=data});
13.17 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(self), __data__), (__attr) {.seqvalue=data});
13.18 __newdata_sequence(number, data, args);
13.19 return self;
13.20 }
13.21 @@ -97,7 +97,7 @@
13.22
13.23 /* Store a reference to the data in the object's __data__ attribute. */
13.24
13.25 - __store_via_attr_ref(__get_object_attr_ref(__VALUE(self), __data__), (__attr) {.seqvalue=data});
13.26 + __store_via_attr_ref__(__get_object_attr_ref(__VALUE(self), __data__), (__attr) {.seqvalue=data});
13.27 __newdata_sequence(number, data, args);
13.28 return self;
13.29 }
13.30 @@ -315,7 +315,7 @@
13.31
13.32 void __SETDEFAULT(__ref obj, int pos, __attr value)
13.33 {
13.34 - __store_via_attr_ref(__get_object_attr_ref__(obj, __FUNCTION_INSTANCE_SIZE + pos), value);
13.35 + __store_via_attr_ref__(__get_object_attr_ref__(obj, __FUNCTION_INSTANCE_SIZE + pos), value);
13.36 }
13.37
13.38 __attr __GETDEFAULT(__ref obj, int pos)
14.1 --- a/templates/progops.h Sun Sep 03 00:24:29 2023 +0200
14.2 +++ b/templates/progops.h Tue Sep 05 21:16:49 2023 +0200
14.3 @@ -91,6 +91,6 @@
14.4 /* Operations for accessing trailing data. */
14.5
14.6 #define __get_trailing_data(ATTR, TYPE) (((__OBJTYPE(TYPE) *) ((ATTR).value))->trailing)
14.7 -#define __set_trailing_data(ATTR, TYPE, VALUE) ((__OBJTYPE(TYPE) *) ((ATTR).value))->trailing = VALUE;
14.8 +#define __set_trailing_data(ATTR, TYPE, VALUE) ((__OBJTYPE(TYPE) *) ((ATTR).value))->trailing = VALUE
14.9
14.10 #endif /* __PROGOPS_H__ */
15.1 --- a/templates/types.h Sun Sep 03 00:24:29 2023 +0200
15.2 +++ b/templates/types.h Tue Sep 05 21:16:49 2023 +0200
15.3 @@ -115,8 +115,6 @@
15.4
15.5 } __obj;
15.6
15.7 -#define __INSTANCE_SIZE(NUMBER) ((NUMBER) * sizeof(__attr) + sizeof(__table *) + sizeof(__ppos))
15.8 -
15.9 /* Fragments are simple collections of attributes employed by sequence types.
15.10 They provide the basis of lists and tuples. */
15.11
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
16.2 +++ b/tests/values.py Tue Sep 05 21:16:49 2023 +0200
16.3 @@ -0,0 +1,7 @@
16.4 +def test(a):
16.5 + a = a - 1.0
16.6 + return a
16.7 +
16.8 +x = 2.0
16.9 +print test(x) # 1.0
16.10 +print x # 2.0
17.1 --- a/translator.py Sun Sep 03 00:24:29 2023 +0200
17.2 +++ b/translator.py Tue Sep 05 21:16:49 2023 +0200
17.3 @@ -1766,9 +1766,17 @@
17.4 # objects, with only floats supporting replaceable values.
17.5
17.6 if expr:
17.7 - target_ref = TrResolvedNameRef(n.name, ref, is_global=is_global,
17.8 - location=location)
17.9 - self.result_target_name = str(target_ref)
17.10 + # Prevent parameters from becoming result targets. Otherwise, they
17.11 + # may inadvertently cause the modification of the supplied object.
17.12 +
17.13 + parameters = self.importer.function_parameters.get(path)
17.14 +
17.15 + if not parameters or n.name not in parameters:
17.16 + target_ref = TrResolvedNameRef(n.name, ref, is_global=is_global,
17.17 + location=location)
17.18 + self.result_target_name = str(target_ref)
17.19 + else:
17.20 + self.result_target_name = None
17.21
17.22 # Expression processing is deferred until after any result target has
17.23 # been set.
18.1 --- a/transresults.py Sun Sep 03 00:24:29 2023 +0200
18.2 +++ b/transresults.py Tue Sep 05 21:16:49 2023 +0200
18.3 @@ -101,6 +101,8 @@
18.4
18.5 if self.expr:
18.6
18.7 + store = self.attrname != "__data__" and "__store_via_attr_ref" or "__store_via_attr_ref__"
18.8 +
18.9 # Eliminate assignments between constants.
18.10
18.11 if self.static_ref and self.expr.static():
18.12 @@ -109,14 +111,14 @@
18.13 # Qualified names must be converted into parent-relative assignments.
18.14
18.15 elif self.parent:
18.16 - return "__store_via_attr_ref(__get_object_attr_ref(&%s, %s), %s)" % (
18.17 - encode_path(self.parent), self.attrname, self.expr)
18.18 + return "%s(__get_object_attr_ref(&%s, %s), %s)" % (
18.19 + store, encode_path(self.parent), self.attrname, self.expr)
18.20
18.21 # All other assignments involve the names as they were given.
18.22 # To support value replacement, a special operation is used.
18.23
18.24 else:
18.25 - return "__set_attr(&%s, %s)" % (self.attrname, self.expr)
18.26 + return "%s(&%s, %s)" % (store, self.attrname, self.expr)
18.27
18.28 # Expressions.
18.29