1.1 --- a/generator.py Thu Nov 04 23:44:29 2021 +0100
1.2 +++ b/generator.py Thu Nov 04 23:57:33 2021 +0100
1.3 @@ -69,7 +69,8 @@
1.4 # Data types with a trailing data member of the given native types.
1.5
1.6 trailing_data_types = {
1.7 - float_type : "double",
1.8 + float_type : "__float",
1.9 + int_type : "__int",
1.10 }
1.11
1.12 def __init__(self, importer, optimiser, output):
1.13 @@ -377,11 +378,6 @@
1.14 for constant, n in self.optimiser.constants.items():
1.15 self.make_literal_constant(f_decls, f_defs, n, constant)
1.16
1.17 - # Generate a common integer instance object, referenced when integer
1.18 - # attributes are accessed.
1.19 -
1.20 - self.make_common_integer(f_decls, f_defs)
1.21 -
1.22 # Finish the main source file.
1.23
1.24 self.write_main_program(f_code, f_signatures)
1.25 @@ -575,11 +571,6 @@
1.26
1.27 value, value_type, encoding = constant
1.28
1.29 - # Do not generate individual integer constants.
1.30 -
1.31 - if value_type == self.int_type:
1.32 - return
1.33 -
1.34 const_path = encode_literal_constant(n)
1.35 structure_name = encode_literal_reference(n)
1.36
1.37 @@ -602,16 +593,6 @@
1.38
1.39 self.make_constant(f_decls, f_defs, ref, attr_path, structure_name)
1.40
1.41 - def make_common_integer(self, f_decls, f_defs):
1.42 -
1.43 - """
1.44 - Write common integer instance details to 'f_decls' (to declare a
1.45 - structure) and to 'f_defs' (to define the contents).
1.46 - """
1.47 -
1.48 - ref = Reference("<instance>", self.int_type)
1.49 - self.make_constant(f_decls, f_defs, ref, "__common_integer", "__common_integer_obj")
1.50 -
1.51 def make_constant(self, f_decls, f_defs, ref, const_path, structure_name, data=None, encoding=None):
1.52
1.53 """
1.54 @@ -1202,13 +1183,6 @@
1.55 # Obtain a constant value directly assigned to the attribute.
1.56
1.57 if self.optimiser.constant_numbers.has_key(alias):
1.58 -
1.59 - # Encode integer constants differently.
1.60 -
1.61 - value, value_type, encoding = self.importer.all_constant_values[alias]
1.62 - if value_type == self.int_type:
1.63 - return "__INTVALUE(%s) /* %s */" % (value, name)
1.64 -
1.65 constant_number = self.optimiser.constant_numbers[alias]
1.66 constant_value = encode_literal_constant(constant_number)
1.67 return "%s /* %s */" % (constant_value, name)
2.1 --- a/lib/native/__init__.py Thu Nov 04 23:44:29 2021 +0100
2.2 +++ b/lib/native/__init__.py Thu Nov 04 23:57:33 2021 +0100
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Native library functions.
2.6
2.7 -Copyright (C) 2011, 2015-2018, 2021 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2011, 2015, 2016, 2017, 2018, 2021 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
3.1 --- a/templates/native/common.c Thu Nov 04 23:44:29 2021 +0100
3.2 +++ b/templates/native/common.c Thu Nov 04 23:57:33 2021 +0100
3.3 @@ -26,6 +26,14 @@
3.4
3.5 /* Utility functions. */
3.6
3.7 +__attr __new_int(__int n)
3.8 +{
3.9 + /* Create a new int and set the trailing data. */
3.10 + __attr attr = __NEWINSTANCEIM(__builtins___int_int);
3.11 + __set_trailing_data(attr, __builtins___int_int, n);
3.12 + return attr;
3.13 +}
3.14 +
3.15 __attr __new_str(char *s, __int size)
3.16 {
3.17 /* Create a new string and mutate the __data__, __size__ and __key__ attributes. */
3.18 @@ -44,7 +52,7 @@
3.19 return attr;
3.20 }
3.21
3.22 -__attr __new_float(double n)
3.23 +__attr __new_float(__float n)
3.24 {
3.25 /* Create a new float and set the trailing data. */
3.26 __attr attr = __NEWINSTANCEIM(__builtins___float_float);
4.1 --- a/templates/native/common.h Thu Nov 04 23:44:29 2021 +0100
4.2 +++ b/templates/native/common.h Thu Nov 04 23:57:33 2021 +0100
4.3 @@ -21,15 +21,12 @@
4.4
4.5 #include "types.h"
4.6
4.7 -/* Utility macro for the special integer representation. */
4.8 -
4.9 -#define __new_int(VALUE) __INTVALUE(VALUE)
4.10 -
4.11 /* Utility functions. */
4.12
4.13 +__attr __new_int(__int n);
4.14 __attr __new_str(char *s, __int size);
4.15 __attr __new_list(__fragment *f);
4.16 -__attr __new_float(double n);
4.17 +__attr __new_float(__float n);
4.18 __fragment *__fragment_append(__fragment *data, __attr value);
4.19
4.20 #endif /* __NATIVE_COMMON_H__ */
5.1 --- a/templates/native/float.c Thu Nov 04 23:44:29 2021 +0100
5.2 +++ b/templates/native/float.c Thu Nov 04 23:57:33 2021 +0100
5.3 @@ -1,6 +1,6 @@
5.4 /* Native functions for floating point operations.
5.5
5.6 -Copyright (C) 2016, 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk>
5.7 +Copyright (C) 2016, 2017, 2018, 2019, 2021 Paul Boddie <paul@boddie.org.uk>
5.8
5.9 This program is free software; you can redistribute it and/or modify it under
5.10 the terms of the GNU General Public License as published by the Free Software
5.11 @@ -28,9 +28,9 @@
5.12 #include "progtypes.h"
5.13 #include "main.h"
5.14
5.15 -/* Conversion of trailing data to a double-precision floating point number. */
5.16 +/* Conversion of trailing data to a floating point number. */
5.17
5.18 -static double __TOFLOAT(__attr attr)
5.19 +static __float __TOFLOAT(__attr attr)
5.20 {
5.21 return __get_trailing_data(attr, __builtins___float_float);
5.22 }
5.23 @@ -38,7 +38,7 @@
5.24 /* Numeric formatting using snprintf.
5.25 NOTE: This might be moved elsewhere and used by other types. */
5.26
5.27 -static __attr format_number(double n, int size)
5.28 +static __attr format_number(__float n, int size)
5.29 {
5.30 char *s = (char *) __ALLOCATE(size, sizeof(char));
5.31 int digits;
5.32 @@ -48,7 +48,7 @@
5.33
5.34 while (1)
5.35 {
5.36 - digits = snprintf(s, size, "%f", n);
5.37 + digits = snprintf(s, size, "%f", (double) n);
5.38
5.39 if (digits < size)
5.40 {
5.41 @@ -68,56 +68,56 @@
5.42 __attr __fn_native_float_float_add(__attr __self, __attr self, __attr other)
5.43 {
5.44 /* self and other interpreted as float */
5.45 - double i = __TOFLOAT(self);
5.46 - double j = __TOFLOAT(other);
5.47 + __float i = __TOFLOAT(self);
5.48 + __float j = __TOFLOAT(other);
5.49 return __new_float(i + j);
5.50 }
5.51
5.52 __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other)
5.53 {
5.54 /* self and other interpreted as float */
5.55 - double i = __TOFLOAT(self);
5.56 - double j = __TOFLOAT(other);
5.57 + __float i = __TOFLOAT(self);
5.58 + __float j = __TOFLOAT(other);
5.59 return __new_float(i - j);
5.60 }
5.61
5.62 __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other)
5.63 {
5.64 /* self and other interpreted as float */
5.65 - double i = __TOFLOAT(self);
5.66 - double j = __TOFLOAT(other);
5.67 + __float i = __TOFLOAT(self);
5.68 + __float j = __TOFLOAT(other);
5.69 return __new_float(i * j);
5.70 }
5.71
5.72 __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other)
5.73 {
5.74 /* self and other interpreted as float */
5.75 - double i = __TOFLOAT(self);
5.76 - double j = __TOFLOAT(other);
5.77 + __float i = __TOFLOAT(self);
5.78 + __float j = __TOFLOAT(other);
5.79 return __new_float(i / j);
5.80 }
5.81
5.82 __attr __fn_native_float_float_mod(__attr __self, __attr self, __attr other)
5.83 {
5.84 /* self and other interpreted as float */
5.85 - double i = __TOFLOAT(self);
5.86 - double j = __TOFLOAT(other);
5.87 + __float i = __TOFLOAT(self);
5.88 + __float j = __TOFLOAT(other);
5.89 return __new_float(fmod(i, j));
5.90 }
5.91
5.92 __attr __fn_native_float_float_neg(__attr __self, __attr self)
5.93 {
5.94 /* self interpreted as float */
5.95 - double i = __TOFLOAT(self);
5.96 + __float i = __TOFLOAT(self);
5.97 return __new_float(-i);
5.98 }
5.99
5.100 __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other)
5.101 {
5.102 /* self and other interpreted as float */
5.103 - double i = __TOFLOAT(self);
5.104 - double j = __TOFLOAT(other);
5.105 - double result;
5.106 + __float i = __TOFLOAT(self);
5.107 + __float j = __TOFLOAT(other);
5.108 + __float result;
5.109
5.110 errno = 0;
5.111 result = pow(i, j);
5.112 @@ -134,8 +134,8 @@
5.113 __attr __fn_native_float_float_le(__attr __self, __attr self, __attr other)
5.114 {
5.115 /* self and other interpreted as float */
5.116 - double i = __TOFLOAT(self);
5.117 - double j = __TOFLOAT(other);
5.118 + __float i = __TOFLOAT(self);
5.119 + __float j = __TOFLOAT(other);
5.120
5.121 /* Return a boolean result. */
5.122 return i <= j ? __builtins___boolean_True : __builtins___boolean_False;
5.123 @@ -144,8 +144,8 @@
5.124 __attr __fn_native_float_float_lt(__attr __self, __attr self, __attr other)
5.125 {
5.126 /* self and other interpreted as float */
5.127 - double i = __TOFLOAT(self);
5.128 - double j = __TOFLOAT(other);
5.129 + __float i = __TOFLOAT(self);
5.130 + __float j = __TOFLOAT(other);
5.131
5.132 /* Return a boolean result. */
5.133 return i < j ? __builtins___boolean_True : __builtins___boolean_False;
5.134 @@ -154,8 +154,8 @@
5.135 __attr __fn_native_float_float_ge(__attr __self, __attr self, __attr other)
5.136 {
5.137 /* self and other interpreted as float */
5.138 - double i = __TOFLOAT(self);
5.139 - double j = __TOFLOAT(other);
5.140 + __float i = __TOFLOAT(self);
5.141 + __float j = __TOFLOAT(other);
5.142
5.143 /* Return a boolean result. */
5.144 return i >= j ? __builtins___boolean_True : __builtins___boolean_False;
5.145 @@ -164,8 +164,8 @@
5.146 __attr __fn_native_float_float_gt(__attr __self, __attr self, __attr other)
5.147 {
5.148 /* self and other interpreted as float */
5.149 - double i = __TOFLOAT(self);
5.150 - double j = __TOFLOAT(other);
5.151 + __float i = __TOFLOAT(self);
5.152 + __float j = __TOFLOAT(other);
5.153
5.154 /* Return a boolean result. */
5.155 return i > j ? __builtins___boolean_True : __builtins___boolean_False;
5.156 @@ -174,8 +174,8 @@
5.157 __attr __fn_native_float_float_eq(__attr __self, __attr self, __attr other)
5.158 {
5.159 /* self and other interpreted as float */
5.160 - double i = __TOFLOAT(self);
5.161 - double j = __TOFLOAT(other);
5.162 + __float i = __TOFLOAT(self);
5.163 + __float j = __TOFLOAT(other);
5.164
5.165 /* Return a boolean result. */
5.166 return i == j ? __builtins___boolean_True : __builtins___boolean_False;
5.167 @@ -184,8 +184,8 @@
5.168 __attr __fn_native_float_float_ne(__attr __self, __attr self, __attr other)
5.169 {
5.170 /* self and other interpreted as float */
5.171 - double i = __TOFLOAT(self);
5.172 - double j = __TOFLOAT(other);
5.173 + __float i = __TOFLOAT(self);
5.174 + __float j = __TOFLOAT(other);
5.175
5.176 /* Return a boolean result. */
5.177 return i != j ? __builtins___boolean_True : __builtins___boolean_False;
5.178 @@ -194,7 +194,7 @@
5.179 __attr __fn_native_float_float_str(__attr __self, __attr self)
5.180 {
5.181 /* self interpreted as float */
5.182 - double i = __TOFLOAT(self);
5.183 + __float i = __TOFLOAT(self);
5.184
5.185 /* Return a new string. */
5.186 return format_number(i, 64);
5.187 @@ -203,7 +203,7 @@
5.188 __attr __fn_native_float_float_int(__attr __self, __attr self)
5.189 {
5.190 /* self interpreted as float */
5.191 - double i = __TOFLOAT(self);
5.192 + __float i = __TOFLOAT(self);
5.193
5.194 /* NOTE: Test for conversion failure. */
5.195 return __new_int((int) i);
6.1 --- a/templates/native/identity.c Thu Nov 04 23:44:29 2021 +0100
6.2 +++ b/templates/native/identity.c Thu Nov 04 23:57:33 2021 +0100
6.3 @@ -1,6 +1,6 @@
6.4 /* Native functions for identity operations.
6.5
6.6 -Copyright (C) 2016, 2017 Paul Boddie <paul@boddie.org.uk>
6.7 +Copyright (C) 2016, 2017, 2021 Paul Boddie <paul@boddie.org.uk>
6.8
6.9 This program is free software; you can redistribute it and/or modify it under
6.10 the terms of the GNU General Public License as published by the Free Software
6.11 @@ -28,14 +28,14 @@
6.12
6.13 __attr __fn_native_identity_is_(__attr __self, __attr x, __attr y)
6.14 {
6.15 - /* NOTE: value member assumed equivalent to intvalue for comparison. */
6.16 + /* NOTE: value member assumed equivalent to rawvalue for comparison. */
6.17
6.18 return x.value == y.value ? __builtins___boolean_True : __builtins___boolean_False;
6.19 }
6.20
6.21 __attr __fn_native_identity_is_not(__attr __self, __attr x, __attr y)
6.22 {
6.23 - /* NOTE: value member assumed equivalent to intvalue for comparison. */
6.24 + /* NOTE: value member assumed equivalent to rawvalue for comparison. */
6.25
6.26 return x.value != y.value ? __builtins___boolean_True : __builtins___boolean_False;
6.27 }
7.1 --- a/templates/ops.c Thu Nov 04 23:44:29 2021 +0100
7.2 +++ b/templates/ops.c Thu Nov 04 23:57:33 2021 +0100
7.3 @@ -1,6 +1,6 @@
7.4 /* Common operations.
7.5
7.6 -Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
7.7 +Copyright (C) 2015, 2016, 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk>
7.8
7.9 This program is free software; you can redistribute it and/or modify it under
7.10 the terms of the GNU General Public License as published by the Free Software
7.11 @@ -27,10 +27,7 @@
7.12
7.13 __ref __VALUE(__attr attr)
7.14 {
7.15 - if (!__INTEGER(attr))
7.16 - return attr.value;
7.17 - else
7.18 - return (__ref) &__common_integer_obj;
7.19 + return attr.value;
7.20 }
7.21
7.22 /* Basic structure tests. */
8.1 --- a/templates/progops.c Thu Nov 04 23:44:29 2021 +0100
8.2 +++ b/templates/progops.c Thu Nov 04 23:57:33 2021 +0100
8.3 @@ -338,3 +338,24 @@
8.4 value == (__ref) &__predefined___builtins___boolean_False ? 0 :
8.5 __VALUE(__fn___builtins___boolean_bool(__NULL, attr)) == (__ref) &__predefined___builtins___boolean_True;
8.6 }
8.7 +
8.8 +/* Conversion of trailing data to an integer. */
8.9 +
8.10 +__int __TOINT(__attr attr)
8.11 +{
8.12 + return __get_trailing_data(attr, __builtins___int_int);
8.13 +}
8.14 +
8.15 +/* Instance test functions, to be replaced by tagged pointer usage. */
8.16 +
8.17 +int __INTEGER(__attr attr)
8.18 +{
8.19 + __ref value = __VALUE(attr);
8.20 + return __get_class(value) == &__builtins___int_int;
8.21 +}
8.22 +
8.23 +int __FLOAT(__attr attr)
8.24 +{
8.25 + __ref value = __VALUE(attr);
8.26 + return __get_class(value) == &__builtins___float_float;
8.27 +}
9.1 --- a/templates/progops.h Thu Nov 04 23:44:29 2021 +0100
9.2 +++ b/templates/progops.h Thu Nov 04 23:57:33 2021 +0100
9.3 @@ -93,4 +93,13 @@
9.4 #define __get_trailing_data(ATTR, TYPE) (((__OBJTYPE(TYPE) *) ((ATTR).value))->trailing)
9.5 #define __set_trailing_data(ATTR, TYPE, VALUE) ((__OBJTYPE(TYPE) *) ((ATTR).value))->trailing = VALUE;
9.6
9.7 +/* Specialised trailing data functions. */
9.8 +
9.9 +__int __TOINT(__attr attr);
9.10 +
9.11 +/* Instance test functions, to be replaced by tagged pointer usage. */
9.12 +
9.13 +int __INTEGER(__attr attr);
9.14 +int __FLOAT(__attr attr);
9.15 +
9.16 #endif /* __PROGOPS_H__ */
10.1 --- a/templates/types.h Thu Nov 04 23:44:29 2021 +0100
10.2 +++ b/templates/types.h Thu Nov 04 23:57:33 2021 +0100
10.3 @@ -74,6 +74,11 @@
10.4 /* Introduce an integer type that is interoperable with the size type. */
10.5
10.6 typedef ssize_t __int;
10.7 +typedef size_t __uint;
10.8 +
10.9 +/* Introduce a floating point type. */
10.10 +
10.11 +typedef _Float64 __float;
10.12
10.13 /* Attribute value interpretations. */
10.14
10.15 @@ -82,7 +87,7 @@
10.16 /* General attribute members. */
10.17
10.18 __ref value; /* attribute value */
10.19 - __int intvalue; /* integer value data (shifted value, tagged) */
10.20 + uintptr_t rawvalue; /* raw attribute value used to test tagging */
10.21
10.22 /* Special case attribute members. */
10.23
10.24 @@ -127,8 +132,13 @@
10.25
10.26 #define __NUM_TAG_BITS 2
10.27 #define __TAG_INT 0b01
10.28 +#define __TAG_FLOAT 0b10
10.29 #define __TAG_MASK 0b11
10.30 -#define __INTEGER(ATTR) (((ATTR).intvalue & __TAG_MASK) == __TAG_INT)
10.31 +
10.32 +#if 0
10.33 +#define __INTEGER(ATTR) (((ATTR).rawvalue & __TAG_MASK) == __TAG_INT)
10.34 +#define __FLOAT(ATTR) (((ATTR).rawvalue & __TAG_MASK) == __TAG_FLOAT)
10.35 +#endif
10.36
10.37 /* Attribute value setting. */
10.38
10.39 @@ -136,12 +146,10 @@
10.40 #define __NULL __ATTRVALUE(0)
10.41 #define __SETNULL(ATTR) ((ATTR).value = 0)
10.42
10.43 -/* Attribute as instance setting. */
10.44 +/* Value limits. */
10.45
10.46 -#define __INTVALUE(VALUE) ((__attr) {.intvalue=(((__int) VALUE) << __NUM_TAG_BITS) | __TAG_INT})
10.47 -#define __TOINT(ATTR) ((ATTR).intvalue >> __NUM_TAG_BITS)
10.48 -#define __MAXINT ((((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS)) - 1)
10.49 -#define __MININT (-(((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS)))
10.50 +#define __MAXINT ((((__uint) 1) << ((sizeof(__int) * 8) - 1)) - 1)
10.51 +#define __MININT (-(((__uint) 1) << ((sizeof(__int) * 8) - 1)))
10.52
10.53 /* Argument lists. */
10.54
11.1 --- a/transresults.py Thu Nov 04 23:44:29 2021 +0100
11.2 +++ b/transresults.py Thu Nov 04 23:57:33 2021 +0100
11.3 @@ -3,7 +3,7 @@
11.4 """
11.5 Translation result abstractions.
11.6
11.7 -Copyright (C) 2016, 2017, 2018 Paul Boddie <paul@boddie.org.uk>
11.8 +Copyright (C) 2016, 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk>
11.9
11.10 This program is free software; you can redistribute it and/or modify it under
11.11 the terms of the GNU General Public License as published by the Free Software
11.12 @@ -139,13 +139,7 @@
11.13 "A constant value reference in the translation."
11.14
11.15 def __str__(self):
11.16 -
11.17 - # NOTE: Should reference a common variable for the type name.
11.18 -
11.19 - if self.ref.get_origin() == "__builtins__.int.int":
11.20 - return "__INTVALUE(%s)" % self.value
11.21 - else:
11.22 - return encode_literal_constant(self.number)
11.23 + return encode_literal_constant(self.number)
11.24
11.25 class TrLiteralSequenceRef(LiteralSequenceRef):
11.26