1.1 --- a/templates/native/common.c Thu Nov 04 23:44:29 2021 +0100
1.2 +++ b/templates/native/common.c Tue Aug 29 01:46:23 2023 +0200
1.3 @@ -1,6 +1,6 @@
1.4 /* Common operations for native functions.
1.5
1.6 -Copyright (C) 2016, 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk>
1.7 +Copyright (C) 2016, 2017, 2018, 2021, 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 @@ -44,12 +44,33 @@
1.12 return attr;
1.13 }
1.14
1.15 -__attr __new_float(double n)
1.16 +/* Temporary result target. */
1.17 +
1.18 +__attr __new_float(__attr __result, double n)
1.19 {
1.20 - /* Create a new float and set the trailing data. */
1.21 - __attr attr = __NEWINSTANCEIM(__builtins___float_float);
1.22 + __attr attr;
1.23 +
1.24 + /* Identify any suitable target for value replacement. The assumption is
1.25 + that locally referenced float objects are always replaceable since they
1.26 + will be appropriately initialised. */
1.27 +
1.28 + __ref obj = __VALUE(__result);
1.29 + int replace = (obj != NULL) && __is_instance(obj) && (__get_class(obj) == &__builtins___float_float);
1.30 +
1.31 + if (replace)
1.32 + attr = __result;
1.33 +
1.34 + /* Alternatively, create a new float. */
1.35 + else
1.36 + attr = __NEWINSTANCEIM(__builtins___float_float);
1.37 +
1.38 + /* Set the trailing data, replacing an existing value if applicable. */
1.39 __set_trailing_data(attr, __builtins___float_float, n);
1.40 - return attr;
1.41 +
1.42 + if (replace)
1.43 + return __REPLACEMENT(attr);
1.44 + else
1.45 + return attr;
1.46 }
1.47
1.48 __fragment *__fragment_append(__fragment *data, __attr value)