public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: crowl@google.com (Lawrence Crowl)
To: reply@codereview.appspotmail.com, dnovillo@google.com,
	tromey@redhat.com,        gcc-patches@gcc.gnu.org
Subject: [pph] Macro Validation Correction (issue4425041)
Date: Fri, 15 Apr 2011 03:39:00 -0000	[thread overview]
Message-ID: <20110415020131.0021F222679@jade.mtv.corp.google.com> (raw)


An earlier change reduced the number of bits in cpp_hashnode.directive_index
from 7 to 5, as that was sufficient for indexing the directives.  Tom Tromey
asked for a static check on the size.  This patch adds that check.

Unfortunately, five bits are not sufficient for the alternate use of
cpp_hashnode.directive_index as a named operator index.  So, I have reverted
the number of bits from five back to seven.  As a result, we now have 34 bits
in small fields, and the size of cpp_hashnode will increase from two to three
words on 32-bit systems.  The size on 64-bit systems remains unchanged because
these bits go into an alignment gap.

Index: libcpp/ChangeLog.pph

2011-04-14  Lawrence Crowl  <crowl@google.com>

	* include/cpplib.h (cpp_hashnode):  Use a macro for the number of bits
	in the directive_index bitfield.  Change the number of bits back to 7.
	* directives.c (DIRECTIVE_TABLE):  Add a gcc-build-time check that the
	index of the directive table fits within the number of bits above.
	* init.c (operator_array):  Separate the named operator table into a
	macro.  Use it in operator_array.  Test that the values fit within the
	number of bits above.



Index: libcpp/directives.c
===================================================================
--- libcpp/directives.c	(revision 172457)
+++ libcpp/directives.c	(working copy)
@@ -137,10 +137,7 @@ static void cpp_pop_definition (cpp_read
    pcmcia-cs-3.0.9).  This is no longer important as directive lookup
    is now O(1).  All extensions other than #warning, #include_next,
    and #import are deprecated.  The name is where the extension
-   appears to have come from.
-
-   Make sure the bitfield directive_index in include/cpplib.h is large
-   enough to index the entire table.  */
+   appears to have come from.  */
 
 #define DIRECTIVE_TABLE							\
 D(define,	T_DEFINE = 0,	KANDR,     IN_I)	   /* 270554 */ \
@@ -181,6 +178,13 @@ enum
 };
 #undef D
 
+/* Make sure the bitfield directive_index in include/cpplib.h is large
+   enough to index the entire table.  */
+
+unsigned char too_many_directives_for_bitfield[
+        N_DIRECTIVES <= (1 << CPP_HASHNODE_INDEX_BITS)
+        ? 1 : -1];
+
 #define D(name, t, origin, flags) \
 { do_##name, (const uchar *) #name, \
   sizeof #name - 1, origin, flags },
Index: libcpp/include/cpplib.h
===================================================================
--- libcpp/include/cpplib.h	(revision 172457)
+++ libcpp/include/cpplib.h	(working copy)
@@ -650,12 +650,14 @@ union GTY(()) _cpp_hashnode_value {
   unsigned short GTY ((tag ("NTV_ARGUMENT"))) arg_index;
 };
 
+#define CPP_HASHNODE_INDEX_BITS 7
+
 struct GTY(()) cpp_hashnode {
   struct ht_identifier ident;
   unsigned int is_directive : 1;
-  unsigned int directive_index : 5;	/* If is_directive,
-					   then index into directive table.
-					   Otherwise, a NODE_OPERATOR.  */
+  unsigned int directive_index : CPP_HASHNODE_INDEX_BITS;
+	/* If is_directive, then index into directive table.
+	   Otherwise, a NODE_OPERATOR.  */
   unsigned int used_by_directive : 1;	/* In an active #if, #define etc.  */
   unsigned int expanded_to_text : 1;	/* Produces tokens for parser.  */
   unsigned char rid_code;		/* Rid code - for front ends.  */
Index: libcpp/init.c
===================================================================
--- libcpp/init.c	(revision 172457)
+++ libcpp/init.c	(working copy)
@@ -375,23 +375,34 @@ struct builtin_operator
   const unsigned short value;
 };
 
-#define B(n, t)    { DSC(n), t }
+#define NAMED_OPER_TABLE \
+  B("and",	CPP_AND_AND) \
+  B("and_eq",	CPP_AND_EQ) \
+  B("bitand",	CPP_AND) \
+  B("bitor",	CPP_OR) \
+  B("compl",	CPP_COMPL) \
+  B("not",	CPP_NOT) \
+  B("not_eq",	CPP_NOT_EQ) \
+  B("or",	CPP_OR_OR) \
+  B("or_eq",	CPP_OR_EQ) \
+  B("xor",	CPP_XOR) \
+  B("xor_eq",	CPP_XOR_EQ) \
+
+#define B(n, t)    { DSC(n), t },
 static const struct builtin_operator operator_array[] =
 {
-  B("and",	CPP_AND_AND),
-  B("and_eq",	CPP_AND_EQ),
-  B("bitand",	CPP_AND),
-  B("bitor",	CPP_OR),
-  B("compl",	CPP_COMPL),
-  B("not",	CPP_NOT),
-  B("not_eq",	CPP_NOT_EQ),
-  B("or",	CPP_OR_OR),
-  B("or_eq",	CPP_OR_EQ),
-  B("xor",	CPP_XOR),
-  B("xor_eq",	CPP_XOR_EQ)
+  NAMED_OPER_TABLE
 };
 #undef B
 
+/* Verify that the indicies of the named operators fit within the
+   number of bits available. */
+
+#define B(n, t) unsigned char t ## _too_large_for_bitfield[ \
+		t < (1 << CPP_HASHNODE_INDEX_BITS) ? 1 : -1];
+NAMED_OPER_TABLE
+#undef B
+
 /* Mark the C++ named operators in the hash table.  */
 static void
 mark_named_operators (cpp_reader *pfile, int flags)

--
This patch is available for review at http://codereview.appspot.com/4425041

             reply	other threads:[~2011-04-15  2:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-15  3:39 Lawrence Crowl [this message]
2011-04-16 17:37 ` Diego Novillo
2011-04-18 13:34   ` Tom Tromey
2011-04-18 19:04   ` Lawrence Crowl
2011-04-18 19:23     ` Diego Novillo
2011-04-23  0:50   ` Hans-Peter Nilsson
2011-04-25 18:58     ` Lawrence Crowl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110415020131.0021F222679@jade.mtv.corp.google.com \
    --to=crowl@google.com \
    --cc=dnovillo@google.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=reply@codereview.appspotmail.com \
    --cc=tromey@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).