This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC,PATCH] Builtin macros for sizes of standard types


Hi Mark,

although mentioned in the cc line your email somehow didn't make it 
to the mailing list archive.  I've copied its contents manually:

>> +   builtin_define_with_int_value ("__SIZEOF_VOID_P__",
>> + 		 		  POINTER_SIZE / BITS_PER_UNIT);
>
>If we're going to do this, this should be "__SIZEOF_POINTER__".  The
>value is also valid for sizeof (int *).  Do we support machines with
>function pointers whose sizes are different from function pointers?  If
>so, then, maybe "__SIZEOF_DATA_POINTER__".

I've tried to mimic the macros defined by autoconf (plus double
underscores).  Autoconf defines a SIZEOF_VOID_P but I don't have a strong
preference for it so I changed it to __SIZEOF_POINTER__.

I'm not aware of gcc supporting targets where the size of a function
pointer differs from the size of a data pointer.  I thought that
all pointer types are based on POINTER_SIZE.  If nobody objects
I would stick to __SIZEOF_POINTER__.

>Why not also have macros for "float", "double", and "long double"?  In
>other words, if we're going to put these macros in, we should try to
>support all of the fundamental C types.

Agreed.  I've also added a __SIZEOF_SIZE_T__.

Bootstrapped on i686, s390 and s390x.
No testsuite regressions.

OK for mainline?

Bye,

-Andreas-

2007-01-23  Andreas Krebbel  <krebbel1@de.ibm.com>

	* c-cppbuiltin.c (builtin_define_type_sizeof): New function.
	(c_cpp_builtins): New builtin macros: __SIZEOF_INT__, __SIZEOF_LONG__,
	__SIZEOF_LONG_LONG__, __SIZEOF_SHORT__, __SIZEOF_POINTER__,
	__SIZEOF_SIZE_T__, __SIZEOF_FLOAT__, __SIZEOF_DOUBLE__ and
	__SIZEOF_LONG_DOUBLE__.
	* doc/cpp.texi: Documentation for the new builtin macros added.


2007-01-23  Andreas Krebbel  <krebbel1@de.ibm.com>

	* gcc.c-torture/compile/sizeof-macros-1.c: New testcase.


Index: gcc/c-cppbuiltin.c
===================================================================
*** gcc/c-cppbuiltin.c.orig	2007-01-23 12:06:15.000000000 +0100
--- gcc/c-cppbuiltin.c	2007-01-23 12:06:19.000000000 +0100
*************** static void builtin_define_with_hex_fp_v
*** 58,63 ****
--- 58,64 ----
  static void builtin_define_stdint_macros (void);
  static void builtin_define_type_max (const char *, tree, int);
  static void builtin_define_type_precision (const char *, tree);
+ static void builtin_define_type_sizeof (const char *, tree);
  static void builtin_define_float_constants (const char *, 
  					    const char *,
  					    const char *,
*************** builtin_define_type_precision (const cha
*** 71,76 ****
--- 72,85 ----
    builtin_define_with_int_value (name, TYPE_PRECISION (type));
  }
  
+ /* Define NAME with value TYPE size_unit.  */
+ static void
+ builtin_define_type_sizeof (const char *name, tree type)
+ {
+   builtin_define_with_int_value (name,
+ 				 TREE_INT_CST_LOW (TYPE_SIZE_UNIT (type)));
+ }
+ 
  /* Define the float.h constants for TYPE using NAME_PREFIX, FP_SUFFIX,
     and FP_CAST. */
  static void
*************** c_cpp_builtins (cpp_reader *pfile)
*** 549,554 ****
--- 558,577 ----
    if (flag_openmp)
      cpp_define (pfile, "_OPENMP=200505");
  
+   builtin_define_type_sizeof ("__SIZEOF_INT__", integer_type_node);
+   builtin_define_type_sizeof ("__SIZEOF_LONG__", long_integer_type_node);
+   builtin_define_type_sizeof ("__SIZEOF_LONG_LONG__",
+ 			      long_long_integer_type_node);
+   builtin_define_type_sizeof ("__SIZEOF_SHORT__", short_integer_type_node);
+   builtin_define_type_sizeof ("__SIZEOF_FLOAT__", float_type_node);
+   builtin_define_type_sizeof ("__SIZEOF_DOUBLE__", double_type_node);
+   builtin_define_type_sizeof ("__SIZEOF_LONG_DOUBLE__", long_double_type_node);
+   builtin_define_type_sizeof ("__SIZEOF_SIZE_T__", size_type_node);
+   /* ptr_type_node can't be used here since ptr_mode is only set when
+      toplev calls backend_init which is not done with -E switch.  */
+   builtin_define_with_int_value ("__SIZEOF_POINTER__",
+ 				 POINTER_SIZE / BITS_PER_UNIT);
+ 
    /* A straightforward target hook doesn't work, because of problems
       linking that hook's body when part of non-C front ends.  */
  # define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
Index: gcc/doc/cpp.texi
===================================================================
*** gcc/doc/cpp.texi.orig	2007-01-23 12:06:15.000000000 +0100
--- gcc/doc/cpp.texi	2007-01-23 12:06:19.000000000 +0100
*************** respectively.  They exist to make the st
*** 2079,2084 ****
--- 2079,2097 ----
  work correctly.  You should not use these macros directly; instead, include
  the appropriate headers.
  
+ @item __SIZEOF_INT__
+ @itemx __SIZEOF_LONG__
+ @itemx __SIZEOF_LONG_LONG__
+ @itemx __SIZEOF_SHORT__
+ @itemx __SIZEOF_POINTER__
+ @itemx __SIZEOF_SIZE_T__
+ @itemx __SIZEOF_FLOAT__
+ @itemx __SIZEOF_DOUBLE__
+ @itemx __SIZEOF_LONG_DOUBLE__
+ Defined to the number of bytes of the C standard data types: @code{int},
+ @code{long}, @code{long long}, @code{short}, @code{void *}, @code{size_t},
+ @code{float}, @code{double} and @code{long double}.
+ 
  @item __DEPRECATED
  This macro is defined, with value 1, when compiling a C++ source file
  with warnings about deprecated constructs enabled.  These warnings are
Index: gcc/testsuite/gcc.c-torture/compile/sizeof-macros-1.c
===================================================================
*** /dev/null	1970-01-01 00:00:00.000000000 +0000
--- gcc/testsuite/gcc.c-torture/compile/sizeof-macros-1.c	2007-01-23 13:57:32.000000000 +0100
***************
*** 0 ****
--- 1,12 ----
+ /* This checks the gcc builtin macros defined to the byte
+    sizes of C standard types.  */
+ 
+ int a[sizeof(int) == __SIZEOF_INT__ ? 1 : -1];
+ int b[sizeof(long) == __SIZEOF_LONG__ ? 1 : -1];
+ int c[sizeof(long long) == __SIZEOF_LONG_LONG__ ? 1 : -1];
+ int d[sizeof(short) == __SIZEOF_SHORT__ ? 1 : -1];
+ int e[sizeof(void *) == __SIZEOF_POINTER__ ? 1 : -1];
+ int f[sizeof(__SIZE_TYPE__) == __SIZEOF_SIZE_T__ ? 1 : -1];
+ int g[sizeof(float) == __SIZEOF_FLOAT__ ? 1 : -1];
+ int h[sizeof(double) == __SIZEOF_DOUBLE__ ? 1 : -1];
+ int i[sizeof(long double) == __SIZEOF_LONG_DOUBLE__ ? 1 : -1];


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]