Patch: reduce size of cp_token

Andrew Pinski pinskia@gmail.com
Sat Mar 1 05:37:00 GMT 2008



Sent from my iPhone

On Feb 29, 2008, at 20:12, Tom Tromey <tromey@redhat.com> wrote:

> My earlier patch to remove input_file_stack also removed a field from
> struct cp_token.  This made it possible to reduce the size of this
> struct.
>
> This patch rearranges cp_token.  On my x86 box it now takes 12 bytes
> rather than 16.  This patch reduces the size of cp_token::type by one
> bit, but it also adds a compile-time assertion to ensure that we don't
> overflow this.  I've also moved the 'location' field earlier to get
> better packing on a 64-bit machine, though I did not actually verify
> this in action.  I also did not try to measure how much memory this
> saves.
>
> Bootstrapped & regtested on x86 FC-6.  Ok?
>
> Tom
>
> cp/ChangeLog:
> 2008-02-29  Tom Tromey  <tromey@redhat.com>
>
>    * parser.c (CP_TOKEN_BITS): New define.
>    (struct cp_token) <type>: Use it.
>    (struct cp_token) <ambiguous_p>: Move earlier.
>    (struct cp_token) <location>: Move earlier.
>    (cp_token_size_self_check): Declare.
>    (eof_token): Update.
>
> Index: cp/parser.c
> ===================================================================
> --- cp/parser.c    (revision 132775)
> +++ cp/parser.c    (working copy)
> @@ -58,12 +58,20 @@
>   tree qualifying_scope;
> };
>
> -/* A C++ token.  */
> +/* Number of bits used to represent a token in struct cp_token.  */
> +#define CP_TOKEN_BITS 7
>
> +/* A C++ token.  Note that this is carefully laid out to have the
> +   least amount of padding.  */
> +
> typedef struct cp_token GTY (())
> {
>   /* The kind of token.  */
> -  ENUM_BITFIELD (cpp_ttype) type : 8;
> +  ENUM_BITFIELD (cpp_ttype) type : CP_TOKEN_BITS;
> +  /* True for a CPP_NAME token that is not a keyword (i.e., for which
> +     KEYWORD is RID_MAX) iff this name was looked up and found to be
> +     ambiguous.  An error has already been reported.  */
> +  BOOL_BITFIELD ambiguous_p : 1;
>   /* If this token is a keyword, this value indicates which keyword.
>      Otherwise, this value is RID_MAX.  */
>   ENUM_BITFIELD (rid) keyword : 8;
> @@ -75,10 +83,8 @@
>   BOOL_BITFIELD in_system_header : 1;
>   /* True if this token is from a context where it is implicitly  
> extern "C" */
>   BOOL_BITFIELD implicit_extern_c : 1;
> -  /* True for a CPP_NAME token that is not a keyword (i.e., for which
> -     KEYWORD is RID_MAX) iff this name was looked up and found to be
> -     ambiguous.  An error has already been reported.  */
> -  BOOL_BITFIELD ambiguous_p : 1;
> +  /* The location at which this token was found.  */
> +  location_t location;
>   /* The value associated with this token, if any.  */
>   union cp_token_value {
>     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
> @@ -86,8 +92,6 @@
>     /* Use for all other tokens.  */
>     tree GTY((tag ("0"))) value;
>   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type ==  
> CPP_NESTED_NAME_SPECIFIER)"))) u;
> -  /* The location at which this token was found.  */
> -  location_t location;
> } cp_token;
>
> /* We use a stack of token pointer for saving token sets.  */
> @@ -97,8 +101,7 @@
>
> static cp_token eof_token =
> {
> -  CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL },
> -  0
> +  CPP_EOF, false, RID_MAX, 0, PRAGMA_NONE, 0, false, 0, { NULL }
> };
>
> /* The cp_lexer structure represents the C++ lexer.  It is responsible
> @@ -246,6 +249,9 @@
> /* The number of token types, including C++-specific ones.  */
> #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
>
> +/* Self-check that we have enough bits reserved in cp_token.  */
> +extern int cp_token_size_self_check[(N_CP_TTYPES <= (1 <<  
> CP_TOKEN_BITS)) ? 1 : -1];

Is it better to use a typedef here, just in case a compiler decides to  
emit a reference to the extern variable?

Thanks,
Andrew Pinski



More information about the Gcc-patches mailing list