Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Paul Hilfinger <hilfingr@gnat.com>
To: cagney@gnu.org
Cc: gdb-patches@sources.redhat.com
Subject: Re: [RFA] Setting long long bitfields
Date: Mon, 01 Nov 2004 11:17:00 -0000	[thread overview]
Message-ID: <20041101111749.8CB08F2B98@nile.gnat.com> (raw)
In-Reply-To: <41852B2B.6030305@gnu.org> (message from Andrew Cagney on Sun, 31 Oct 2004 13:12:59 -0500)


I have committed the revised version of this patch, below.

Paul Hilfinger

2004-11-01  Paul N. Hilfinger  <Hilfinger@gnat.com>

	* values.c (modify_field): Correct handling of bit-fields that
	don't fit in 32 bits.  Use unsigned operations throughout and 
	simplify the code a bit.  Document preconditions.

Index: gdb/values.c
===================================================================
RCS file: /cvs/src/src/gdb/values.c,v
retrieving revision 1.71
diff -u -p -r1.71 values.c
--- gdb/values.c	28 Jul 2004 02:46:24 -0000	1.71
+++ gdb/values.c	1 Nov 2004 10:42:29 -0000
@@ -1070,45 +1070,42 @@ unpack_field_as_long (struct type *type,
 /* Modify the value of a bitfield.  ADDR points to a block of memory in
    target byte order; the bitfield starts in the byte pointed to.  FIELDVAL
    is the desired value of the field, in host byte order.  BITPOS and BITSIZE
-   indicate which bits (in target bit order) comprise the bitfield.  */
+   indicate which bits (in target bit order) comprise the bitfield.  
+   Requires 0 < BITSIZE <= lbits, 0 <= BITPOS+BITSIZE <= lbits, and
+   0 <= BITPOS, where lbits is the size of a LONGEST in bits.  */
 
 void
 modify_field (char *addr, LONGEST fieldval, int bitpos, int bitsize)
 {
-  LONGEST oword;
+  ULONGEST oword;
+  ULONGEST mask = (ULONGEST) -1 >> (8 * sizeof (ULONGEST) - bitsize);
 
   /* If a negative fieldval fits in the field in question, chop
      off the sign extension bits.  */
-  if (bitsize < (8 * (int) sizeof (fieldval))
-      && (~fieldval & ~((1 << (bitsize - 1)) - 1)) == 0)
-    fieldval = fieldval & ((1 << bitsize) - 1);
+  if ((~fieldval & ~(mask >> 1)) == 0)
+    fieldval &= mask;
 
   /* Warn if value is too big to fit in the field in question.  */
-  if (bitsize < (8 * (int) sizeof (fieldval))
-      && 0 != (fieldval & ~((1 << bitsize) - 1)))
+  if (0 != (fieldval & ~mask))
     {
       /* FIXME: would like to include fieldval in the message, but
          we don't have a sprintf_longest.  */
       warning ("Value does not fit in %d bits.", bitsize);
 
       /* Truncate it, otherwise adjoining fields may be corrupted.  */
-      fieldval = fieldval & ((1 << bitsize) - 1);
+      fieldval &= mask;
     }
 
-  oword = extract_signed_integer (addr, sizeof oword);
+  oword = extract_unsigned_integer (addr, sizeof oword);
 
   /* Shifting for bit field depends on endianness of the target machine.  */
   if (BITS_BIG_ENDIAN)
     bitpos = sizeof (oword) * 8 - bitpos - bitsize;
 
-  /* Mask out old value, while avoiding shifts >= size of oword */
-  if (bitsize < 8 * (int) sizeof (oword))
-    oword &= ~(((((ULONGEST) 1) << bitsize) - 1) << bitpos);
-  else
-    oword &= ~((~(ULONGEST) 0) << bitpos);
+  oword &= ~(mask << bitpos);
   oword |= fieldval << bitpos;
 
-  store_signed_integer (addr, sizeof oword, oword);
+  store_unsigned_integer (addr, sizeof oword, oword);
 }
 \f
 /* Convert C numbers into newly allocated values */


  parent reply	other threads:[~2004-11-01 11:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-10-31  9:08 Paul Hilfinger
2004-10-31 18:13 ` Andrew Cagney
2004-10-31 18:43   ` Andreas Schwab
2004-10-31 23:04     ` Paul Hilfinger
2004-11-01  2:48       ` Andreas Schwab
2004-11-01 11:17   ` Paul Hilfinger [this message]
2004-11-01  9:42 Paul N. Hilfinger

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=20041101111749.8CB08F2B98@nile.gnat.com \
    --to=hilfingr@gnat.com \
    --cc=cagney@gnu.org \
    --cc=gdb-patches@sources.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