1.1 --- a/templates/ops.c Thu Nov 04 23:44:29 2021 +0100
1.2 +++ b/templates/ops.c Tue Aug 29 01:46:23 2023 +0200
1.3 @@ -1,6 +1,6 @@
1.4 /* Common operations.
1.5
1.6 -Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
1.7 +Copyright (C) 2015, 2016, 2017, 2018, 2023 Paul Boddie <paul@boddie.org.uk>
1.8
1.9 This program is free software; you can redistribute it and/or modify it under
1.10 the terms of the GNU General Public License as published by the Free Software
1.11 @@ -447,3 +447,38 @@
1.12 memcpy(copy, obj, size);
1.13 return copy;
1.14 }
1.15 +
1.16 +/* Return a suitable attribute for a local. For locals that support value
1.17 + replacement, a copied object may be returned in certain cases.
1.18 + NOTE: Only floats are currently supported for value replacement. */
1.19 +
1.20 +void __set_local(volatile __attr *local, __attr attr)
1.21 +{
1.22 + __ref obj;
1.23 +
1.24 + /* Value already replaced in target by an operation. */
1.25 +
1.26 + if (__REPLACING(attr))
1.27 + return;
1.28 +
1.29 + obj = __VALUE(attr);
1.30 +
1.31 + /* Value not replaceable, suitable for direct propagation to a local. */
1.32 +
1.33 + if ((obj == NULL) || !__is_instance(obj) || (__get_class(obj) != &__builtins___float_float))
1.34 + *local = attr;
1.35 +
1.36 + /* Value is replaceable and should be copied to avoid inadvertent
1.37 + sharing. */
1.38 +
1.39 + else
1.40 + *local = __ATTRVALUE(__COPY(obj, sizeof(__obj___builtins___float_float)));
1.41 +}
1.42 +
1.43 +/* Make sure that attributes indicating value replacement are usable as
1.44 + arguments. */
1.45 +
1.46 +__attr __to_arg(__attr attr)
1.47 +{
1.48 + return __REPLACING(attr) ? __REPLACED(attr) : attr;
1.49 +}