diff mbox series

[8/9] pre-processor: add some pre-defined macros

Message ID ff31d963-04ca-6067-feb2-8ea8884ce0b7@ramsayjones.plus.com (mailing list archive)
State Superseded, archived
Headers show
Series misc sparse patches | expand

Commit Message

Ramsay Jones Nov. 19, 2018, 8:53 p.m. UTC
Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
---
 lib.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

Comments

Luc Van Oostenryck Nov. 20, 2018, 12:48 a.m. UTC | #1
On Mon, Nov 19, 2018 at 08:53:38PM +0000, Ramsay Jones wrote:
> 
> Signed-off-by: Ramsay Jones <ramsay@ramsayjones.plus.com>
> ---
>  lib.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 61 insertions(+)
> 
> diff --git a/lib.c b/lib.c
> index 23c3d27..08dc299 100644
> --- a/lib.c
> +++ b/lib.c
> @@ -1248,6 +1262,53 @@ static void predefined_macros(void)
>  	predefined_type_size("LONG", "L", bits_in_long);
>  	predefined_type_size("LONG_LONG", "LL", bits_in_longlong);

I'm good with this but ...
  
> +	predefined_max("INT8", "", 8);
> +	predefined_type("INT8", "signed char");

We don't have a flag for it but in theory sparse supports
target where bits-per-char != 8. Some years ago someone
even used sparse on such a target.

> +	predefined_max("INT16", "", 16);
> +	predefined_type("INT16", "short int");
> +	predefined_max("INT32", "", 32);
> +	predefined_type("INT32", "int");

It would be nice to not assume sizeof(int) == 4
(but yes, I know, a lot of testcases assume it).

> +	predefined_umax("UINT8", "", 8);
> +	predefined_type("UINT8", "unsigned char");
> +	predefined_umax("UINT16", "", 16);
> +	predefined_type("UINT16", "short unsigned int");
> +	predefined_umax("UINT32", "U", 32);
> +	predefined_type("UINT32", "unsigned int");
> +
> +	if (bits_in_pointer == 64) {
> +		predefined_max("INT64", "L", 64);

This is not correct for LLP64 (-msize-llp64).

> +		predefined_type("INT64", "long int");
> +		predefined_max("INTMAX", "L", 64);
> +		predefined_type("INTMAX", "long int");
> +		predefined_max("INTPTR", "L", 64);
> +		predefined_type("INTPTR", "long int");
> +		predefined_max("PTRDIFF", "L", 64);
> +		predefined_type("PTRDIFF", "long int");
> +		predefined_umax("UINT64", "UL", 64);
> +		predefined_type("UINT64", "long unsigned int");
> +		predefined_umax("UINTMAX", "UL", 64);
> +		predefined_type("UINTMAX", "long unsigned int");
> +		predefined_umax("UINTPTR", "UL", 64);
> +		predefined_type("UINTPTR", "long unsigned int");
> +		predefined_umax("SIZE", "UL", 64);

For SIZE, best would be to keep everything together (and for the
suffix it's needed to check if (size_t_ctype == &ulong_ctype)
since, IIRC, some 32bit platforms use unsigned long for size_t).

In fact, it's a whole tricky affair. For example, on PPC32
UINT32_TYPE is defined as 'unsigned long int', PTRDIFF_TYPE is
defined as 'unsigned long int' on x86-x32. It's one of the
reasons I never added all those and left it to cgcc or undefined.

-- Luc
diff mbox series

Patch

diff --git a/lib.c b/lib.c
index 23c3d27..08dc299 100644
--- a/lib.c
+++ b/lib.c
@@ -46,6 +46,8 @@ 
 #include "target.h"
 #include "version.h"
 
+#define ULL_BITS (__CHAR_BIT__ * sizeof(unsigned long long))
+
 int verbose, optimize_level, optimize_size, preprocessing;
 int die_if_error = 0;
 int has_error = 0;
@@ -1161,6 +1163,11 @@  static char **handle_switch(char *arg, char **next)
 	return next;
 }
 
+static void predefined_type(const char *name, const char *type_str)
+{
+	add_pre_buffer("#weak_define __%s_TYPE__ %s\n", name, type_str);
+}
+
 static void predefined_sizeof(const char *name, unsigned bits)
 {
 	char buf[32];
@@ -1177,6 +1184,13 @@  static void predefined_width(const char *name, unsigned bits)
 	predefine(buf, 1, "%d", bits);
 }
 
+static void predefined_umax(const char *name, const char *suffix, unsigned bits)
+{
+	unsigned long long max = (~0ULL) >> (ULL_BITS - bits);
+
+	add_pre_buffer("#weak_define __%s_MAX__ %#llx%s\n", name, max, suffix);
+}
+
 static void predefined_max(const char *name, const char *suffix, unsigned bits)
 {
 	unsigned long long max = (1ULL << (bits - 1 )) - 1;
@@ -1248,6 +1262,53 @@  static void predefined_macros(void)
 	predefined_type_size("LONG", "L", bits_in_long);
 	predefined_type_size("LONG_LONG", "LL", bits_in_longlong);
 
+	predefined_max("INT8", "", 8);
+	predefined_type("INT8", "signed char");
+	predefined_max("INT16", "", 16);
+	predefined_type("INT16", "short int");
+	predefined_max("INT32", "", 32);
+	predefined_type("INT32", "int");
+	predefined_umax("UINT8", "", 8);
+	predefined_type("UINT8", "unsigned char");
+	predefined_umax("UINT16", "", 16);
+	predefined_type("UINT16", "short unsigned int");
+	predefined_umax("UINT32", "U", 32);
+	predefined_type("UINT32", "unsigned int");
+
+	if (bits_in_pointer == 64) {
+		predefined_max("INT64", "L", 64);
+		predefined_type("INT64", "long int");
+		predefined_max("INTMAX", "L", 64);
+		predefined_type("INTMAX", "long int");
+		predefined_max("INTPTR", "L", 64);
+		predefined_type("INTPTR", "long int");
+		predefined_max("PTRDIFF", "L", 64);
+		predefined_type("PTRDIFF", "long int");
+		predefined_umax("UINT64", "UL", 64);
+		predefined_type("UINT64", "long unsigned int");
+		predefined_umax("UINTMAX", "UL", 64);
+		predefined_type("UINTMAX", "long unsigned int");
+		predefined_umax("UINTPTR", "UL", 64);
+		predefined_type("UINTPTR", "long unsigned int");
+		predefined_umax("SIZE", "UL", 64);
+	} else {
+		predefined_max("INT64", "LL", 64);
+		predefined_type("INT64", "long long int");
+		predefined_max("INTMAX", "LL", 64);
+		predefined_type("INTMAX", "long long int");
+		predefined_max("INTPTR", "", 32);
+		predefined_type("INTPTR", "int");
+		predefined_max("PTRDIFF", "", 32);
+		predefined_type("PTRDIFF", "int");
+		predefined_umax("UINT64", "ULL", 64);
+		predefined_type("UINT64", "long long unsigned int");
+		predefined_umax("UINTMAX", "ULL", 64);
+		predefined_type("UINTMAX", "long long unsigned int");
+		predefined_umax("UINTPTR", "U", 32);
+		predefined_type("UINTPTR", "unsigned int");
+		predefined_umax("SIZE", "U", 32);
+	}
+
 	predefined_sizeof("INT128", 128);
 
 	predefined_sizeof("SIZE_T", bits_in_pointer);