# HG changeset patch # User Paul Boddie # Date 1636066653 -3600 # Node ID 329e5533b106d7402ff0cc9df7982c10bdc31e7a # Parent 1e7ccc84119f6b71d82aaf2d697627364b8b711f Store integers and floating point values as trailing data. Although not yet implemented, the idea is to reference them using tagged address attributes, and then to eventually support the copying of these values. diff -r 1e7ccc84119f -r 329e5533b106 generator.py --- a/generator.py Thu Nov 04 23:44:29 2021 +0100 +++ b/generator.py Thu Nov 04 23:57:33 2021 +0100 @@ -69,7 +69,8 @@ # Data types with a trailing data member of the given native types. trailing_data_types = { - float_type : "double", + float_type : "__float", + int_type : "__int", } def __init__(self, importer, optimiser, output): @@ -377,11 +378,6 @@ for constant, n in self.optimiser.constants.items(): self.make_literal_constant(f_decls, f_defs, n, constant) - # Generate a common integer instance object, referenced when integer - # attributes are accessed. - - self.make_common_integer(f_decls, f_defs) - # Finish the main source file. self.write_main_program(f_code, f_signatures) @@ -575,11 +571,6 @@ value, value_type, encoding = constant - # Do not generate individual integer constants. - - if value_type == self.int_type: - return - const_path = encode_literal_constant(n) structure_name = encode_literal_reference(n) @@ -602,16 +593,6 @@ self.make_constant(f_decls, f_defs, ref, attr_path, structure_name) - def make_common_integer(self, f_decls, f_defs): - - """ - Write common integer instance details to 'f_decls' (to declare a - structure) and to 'f_defs' (to define the contents). - """ - - ref = Reference("", self.int_type) - self.make_constant(f_decls, f_defs, ref, "__common_integer", "__common_integer_obj") - def make_constant(self, f_decls, f_defs, ref, const_path, structure_name, data=None, encoding=None): """ @@ -1202,13 +1183,6 @@ # Obtain a constant value directly assigned to the attribute. if self.optimiser.constant_numbers.has_key(alias): - - # Encode integer constants differently. - - value, value_type, encoding = self.importer.all_constant_values[alias] - if value_type == self.int_type: - return "__INTVALUE(%s) /* %s */" % (value, name) - constant_number = self.optimiser.constant_numbers[alias] constant_value = encode_literal_constant(constant_number) return "%s /* %s */" % (constant_value, name) diff -r 1e7ccc84119f -r 329e5533b106 lib/native/__init__.py --- a/lib/native/__init__.py Thu Nov 04 23:44:29 2021 +0100 +++ b/lib/native/__init__.py Thu Nov 04 23:57:33 2021 +0100 @@ -3,7 +3,7 @@ """ Native library functions. -Copyright (C) 2011, 2015-2018, 2021 Paul Boddie +Copyright (C) 2011, 2015, 2016, 2017, 2018, 2021 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software diff -r 1e7ccc84119f -r 329e5533b106 templates/native/common.c --- a/templates/native/common.c Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/native/common.c Thu Nov 04 23:57:33 2021 +0100 @@ -26,6 +26,14 @@ /* Utility functions. */ +__attr __new_int(__int n) +{ + /* Create a new int and set the trailing data. */ + __attr attr = __NEWINSTANCEIM(__builtins___int_int); + __set_trailing_data(attr, __builtins___int_int, n); + return attr; +} + __attr __new_str(char *s, __int size) { /* Create a new string and mutate the __data__, __size__ and __key__ attributes. */ @@ -44,7 +52,7 @@ return attr; } -__attr __new_float(double n) +__attr __new_float(__float n) { /* Create a new float and set the trailing data. */ __attr attr = __NEWINSTANCEIM(__builtins___float_float); diff -r 1e7ccc84119f -r 329e5533b106 templates/native/common.h --- a/templates/native/common.h Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/native/common.h Thu Nov 04 23:57:33 2021 +0100 @@ -21,15 +21,12 @@ #include "types.h" -/* Utility macro for the special integer representation. */ - -#define __new_int(VALUE) __INTVALUE(VALUE) - /* Utility functions. */ +__attr __new_int(__int n); __attr __new_str(char *s, __int size); __attr __new_list(__fragment *f); -__attr __new_float(double n); +__attr __new_float(__float n); __fragment *__fragment_append(__fragment *data, __attr value); #endif /* __NATIVE_COMMON_H__ */ diff -r 1e7ccc84119f -r 329e5533b106 templates/native/float.c --- a/templates/native/float.c Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/native/float.c Thu Nov 04 23:57:33 2021 +0100 @@ -1,6 +1,6 @@ /* Native functions for floating point operations. -Copyright (C) 2016, 2017, 2018, 2019 Paul Boddie +Copyright (C) 2016, 2017, 2018, 2019, 2021 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -28,9 +28,9 @@ #include "progtypes.h" #include "main.h" -/* Conversion of trailing data to a double-precision floating point number. */ +/* Conversion of trailing data to a floating point number. */ -static double __TOFLOAT(__attr attr) +static __float __TOFLOAT(__attr attr) { return __get_trailing_data(attr, __builtins___float_float); } @@ -38,7 +38,7 @@ /* Numeric formatting using snprintf. NOTE: This might be moved elsewhere and used by other types. */ -static __attr format_number(double n, int size) +static __attr format_number(__float n, int size) { char *s = (char *) __ALLOCATE(size, sizeof(char)); int digits; @@ -48,7 +48,7 @@ while (1) { - digits = snprintf(s, size, "%f", n); + digits = snprintf(s, size, "%f", (double) n); if (digits < size) { @@ -68,56 +68,56 @@ __attr __fn_native_float_float_add(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); return __new_float(i + j); } __attr __fn_native_float_float_sub(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); return __new_float(i - j); } __attr __fn_native_float_float_mul(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); return __new_float(i * j); } __attr __fn_native_float_float_div(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); return __new_float(i / j); } __attr __fn_native_float_float_mod(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); return __new_float(fmod(i, j)); } __attr __fn_native_float_float_neg(__attr __self, __attr self) { /* self interpreted as float */ - double i = __TOFLOAT(self); + __float i = __TOFLOAT(self); return __new_float(-i); } __attr __fn_native_float_float_pow(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); - double result; + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); + __float result; errno = 0; result = pow(i, j); @@ -134,8 +134,8 @@ __attr __fn_native_float_float_le(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); /* Return a boolean result. */ return i <= j ? __builtins___boolean_True : __builtins___boolean_False; @@ -144,8 +144,8 @@ __attr __fn_native_float_float_lt(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); /* Return a boolean result. */ return i < j ? __builtins___boolean_True : __builtins___boolean_False; @@ -154,8 +154,8 @@ __attr __fn_native_float_float_ge(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); /* Return a boolean result. */ return i >= j ? __builtins___boolean_True : __builtins___boolean_False; @@ -164,8 +164,8 @@ __attr __fn_native_float_float_gt(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); /* Return a boolean result. */ return i > j ? __builtins___boolean_True : __builtins___boolean_False; @@ -174,8 +174,8 @@ __attr __fn_native_float_float_eq(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); /* Return a boolean result. */ return i == j ? __builtins___boolean_True : __builtins___boolean_False; @@ -184,8 +184,8 @@ __attr __fn_native_float_float_ne(__attr __self, __attr self, __attr other) { /* self and other interpreted as float */ - double i = __TOFLOAT(self); - double j = __TOFLOAT(other); + __float i = __TOFLOAT(self); + __float j = __TOFLOAT(other); /* Return a boolean result. */ return i != j ? __builtins___boolean_True : __builtins___boolean_False; @@ -194,7 +194,7 @@ __attr __fn_native_float_float_str(__attr __self, __attr self) { /* self interpreted as float */ - double i = __TOFLOAT(self); + __float i = __TOFLOAT(self); /* Return a new string. */ return format_number(i, 64); @@ -203,7 +203,7 @@ __attr __fn_native_float_float_int(__attr __self, __attr self) { /* self interpreted as float */ - double i = __TOFLOAT(self); + __float i = __TOFLOAT(self); /* NOTE: Test for conversion failure. */ return __new_int((int) i); diff -r 1e7ccc84119f -r 329e5533b106 templates/native/identity.c --- a/templates/native/identity.c Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/native/identity.c Thu Nov 04 23:57:33 2021 +0100 @@ -1,6 +1,6 @@ /* Native functions for identity operations. -Copyright (C) 2016, 2017 Paul Boddie +Copyright (C) 2016, 2017, 2021 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -28,14 +28,14 @@ __attr __fn_native_identity_is_(__attr __self, __attr x, __attr y) { - /* NOTE: value member assumed equivalent to intvalue for comparison. */ + /* NOTE: value member assumed equivalent to rawvalue for comparison. */ return x.value == y.value ? __builtins___boolean_True : __builtins___boolean_False; } __attr __fn_native_identity_is_not(__attr __self, __attr x, __attr y) { - /* NOTE: value member assumed equivalent to intvalue for comparison. */ + /* NOTE: value member assumed equivalent to rawvalue for comparison. */ return x.value != y.value ? __builtins___boolean_True : __builtins___boolean_False; } diff -r 1e7ccc84119f -r 329e5533b106 templates/ops.c --- a/templates/ops.c Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/ops.c Thu Nov 04 23:57:33 2021 +0100 @@ -1,6 +1,6 @@ /* Common operations. -Copyright (C) 2015, 2016, 2017, 2018 Paul Boddie +Copyright (C) 2015, 2016, 2017, 2018, 2021 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -27,10 +27,7 @@ __ref __VALUE(__attr attr) { - if (!__INTEGER(attr)) - return attr.value; - else - return (__ref) &__common_integer_obj; + return attr.value; } /* Basic structure tests. */ diff -r 1e7ccc84119f -r 329e5533b106 templates/progops.c --- a/templates/progops.c Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/progops.c Thu Nov 04 23:57:33 2021 +0100 @@ -338,3 +338,24 @@ value == (__ref) &__predefined___builtins___boolean_False ? 0 : __VALUE(__fn___builtins___boolean_bool(__NULL, attr)) == (__ref) &__predefined___builtins___boolean_True; } + +/* Conversion of trailing data to an integer. */ + +__int __TOINT(__attr attr) +{ + return __get_trailing_data(attr, __builtins___int_int); +} + +/* Instance test functions, to be replaced by tagged pointer usage. */ + +int __INTEGER(__attr attr) +{ + __ref value = __VALUE(attr); + return __get_class(value) == &__builtins___int_int; +} + +int __FLOAT(__attr attr) +{ + __ref value = __VALUE(attr); + return __get_class(value) == &__builtins___float_float; +} diff -r 1e7ccc84119f -r 329e5533b106 templates/progops.h --- a/templates/progops.h Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/progops.h Thu Nov 04 23:57:33 2021 +0100 @@ -93,4 +93,13 @@ #define __get_trailing_data(ATTR, TYPE) (((__OBJTYPE(TYPE) *) ((ATTR).value))->trailing) #define __set_trailing_data(ATTR, TYPE, VALUE) ((__OBJTYPE(TYPE) *) ((ATTR).value))->trailing = VALUE; +/* Specialised trailing data functions. */ + +__int __TOINT(__attr attr); + +/* Instance test functions, to be replaced by tagged pointer usage. */ + +int __INTEGER(__attr attr); +int __FLOAT(__attr attr); + #endif /* __PROGOPS_H__ */ diff -r 1e7ccc84119f -r 329e5533b106 templates/types.h --- a/templates/types.h Thu Nov 04 23:44:29 2021 +0100 +++ b/templates/types.h Thu Nov 04 23:57:33 2021 +0100 @@ -74,6 +74,11 @@ /* Introduce an integer type that is interoperable with the size type. */ typedef ssize_t __int; +typedef size_t __uint; + +/* Introduce a floating point type. */ + +typedef _Float64 __float; /* Attribute value interpretations. */ @@ -82,7 +87,7 @@ /* General attribute members. */ __ref value; /* attribute value */ - __int intvalue; /* integer value data (shifted value, tagged) */ + uintptr_t rawvalue; /* raw attribute value used to test tagging */ /* Special case attribute members. */ @@ -127,8 +132,13 @@ #define __NUM_TAG_BITS 2 #define __TAG_INT 0b01 +#define __TAG_FLOAT 0b10 #define __TAG_MASK 0b11 -#define __INTEGER(ATTR) (((ATTR).intvalue & __TAG_MASK) == __TAG_INT) + +#if 0 +#define __INTEGER(ATTR) (((ATTR).rawvalue & __TAG_MASK) == __TAG_INT) +#define __FLOAT(ATTR) (((ATTR).rawvalue & __TAG_MASK) == __TAG_FLOAT) +#endif /* Attribute value setting. */ @@ -136,12 +146,10 @@ #define __NULL __ATTRVALUE(0) #define __SETNULL(ATTR) ((ATTR).value = 0) -/* Attribute as instance setting. */ +/* Value limits. */ -#define __INTVALUE(VALUE) ((__attr) {.intvalue=(((__int) VALUE) << __NUM_TAG_BITS) | __TAG_INT}) -#define __TOINT(ATTR) ((ATTR).intvalue >> __NUM_TAG_BITS) -#define __MAXINT ((((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS)) - 1) -#define __MININT (-(((__int) 1) << ((sizeof(__int) * 8) - 1 - __NUM_TAG_BITS))) +#define __MAXINT ((((__uint) 1) << ((sizeof(__int) * 8) - 1)) - 1) +#define __MININT (-(((__uint) 1) << ((sizeof(__int) * 8) - 1))) /* Argument lists. */ diff -r 1e7ccc84119f -r 329e5533b106 transresults.py --- a/transresults.py Thu Nov 04 23:44:29 2021 +0100 +++ b/transresults.py Thu Nov 04 23:57:33 2021 +0100 @@ -3,7 +3,7 @@ """ Translation result abstractions. -Copyright (C) 2016, 2017, 2018 Paul Boddie +Copyright (C) 2016, 2017, 2018, 2021 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -139,13 +139,7 @@ "A constant value reference in the translation." def __str__(self): - - # NOTE: Should reference a common variable for the type name. - - if self.ref.get_origin() == "__builtins__.int.int": - return "__INTVALUE(%s)" % self.value - else: - return encode_literal_constant(self.number) + return encode_literal_constant(self.number) class TrLiteralSequenceRef(LiteralSequenceRef):