linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Given <dg@cowlark.com>
To: linux-sparse@vger.kernel.org
Subject: Re: Pointer arithmetic error
Date: Sun, 29 Jun 2008 01:10:28 +0100	[thread overview]
Message-ID: <4866D2F4.8090109@cowlark.com> (raw)
In-Reply-To: <70318cbf0806271717v42f4b1e0ocbdf9498b1417878@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 812 bytes --]

Christopher Li wrote:
> Your patch is white space damaged.
> 
> While you are there, you might want to consider macro
> 
> bits_to_byte(x)
> byte_to_bits(x)
> 
> To isolate out the conversion. It should generate the same code.
> Marginally more readable.

Yes, good idea. Updated patch enclosed. I've also applied Josh
Triplett's fixes.

(This one's generated manually because I've been totally unable to make
git behave for me. Can anyone suggest a decent getting-started guide?)

-- 
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out
│ how to use my telephone." --- Bjarne Stroustrup

[-- Attachment #1.2: diff --]
[-- Type: text/plain, Size: 7364 bytes --]

diff -rwu ./compile-i386.c ../sparse/compile-i386.c
--- ./compile-i386.c	2008-06-28 22:44:21.706554998 +0100
+++ ../sparse/compile-i386.c	2008-06-28 23:45:21.246554627 +0100
@@ -2081,7 +2081,7 @@
 		insn("pushl", new, NULL,
 		     !framesize ? "begin function call" : NULL);
 
-		framesize += size >> 3;
+		framesize += bits_to_bytes(size);
 	} END_FOR_EACH_PTR_REVERSE(arg);
 
 	fn = expr->fn;
Only in .: diff
diff -rwu ./evaluate.c ../sparse/evaluate.c
--- ./evaluate.c	2008-06-28 22:44:21.706554998 +0100
+++ ../sparse/evaluate.c	2008-06-28 23:50:42.306555440 +0100
@@ -579,7 +579,7 @@
 	}
 
 	/* Get the size of whatever the pointer points to */
-	multiply = base->bit_size >> 3;
+	multiply = bits_to_bytes(base->bit_size);
 
 	if (ctype == &null_ctype)
 		ctype = &ptr_ctype;
@@ -831,7 +831,7 @@
 		struct expression *sub = alloc_expression(expr->pos, EXPR_BINOP);
 		struct expression *div = expr;
 		struct expression *val = alloc_expression(expr->pos, EXPR_VALUE);
-		unsigned long value = lbase->bit_size >> 3;
+		unsigned long value = bits_to_bytes(lbase->bit_size);
 
 		val->ctype = size_t_ctype;
 		val->value = value;
@@ -1591,7 +1591,7 @@
 				e3->op = '+';
 				e3->left = e0;
 				e3->right = alloc_const_expression(expr->pos,
-							expr->r_bitpos >> 3);
+							bits_to_bytes(expr->r_bitpos));
 				e3->ctype = &lazy_ptr_ctype;
 			} else {
 				e3 = e0;
@@ -1727,7 +1727,7 @@
 	} else if (class == TYPE_PTR) {
 		struct symbol *target = examine_pointer_target(ctype);
 		if (!is_function(target))
-			multiply = target->bit_size >> 3;
+			multiply = bits_to_bytes(target->bit_size);
 	}
 
 	if (multiply) {
@@ -1949,7 +1949,7 @@
 			expr->base = deref->base;
 			expr->r_bitpos = deref->r_bitpos;
 		}
-		expr->r_bitpos += offset << 3;
+		expr->r_bitpos += bytes_to_bits(offset);
 		expr->type = EXPR_SLICE;
 		expr->r_nrbits = member->bit_size;
 		expr->r_bitpos += member->bit_offset;
@@ -2037,10 +2037,10 @@
 		return NULL;
 
 	size = type->bit_size;
-	if ((size < 0) || (size & 7))
+	if ((size < 0) || (size & (bits_in_char - 1)))
 		expression_error(expr, "cannot size expression");
 	expr->type = EXPR_VALUE;
-	expr->value = size >> 3;
+	expr->value = bits_to_bytes(size);
 	expr->taint = 0;
 	expr->ctype = size_t_ctype;
 	return size_t_ctype;
@@ -2071,10 +2071,10 @@
 		return NULL;
 	}
 	size = type->bit_size;
-	if (size & 7)
+	if (size & (bits_in_char-1))
 		size = 0;
 	expr->type = EXPR_VALUE;
-	expr->value = size >> 3;
+	expr->value = bits_to_bytes(size);
 	expr->taint = 0;
 	expr->ctype = size_t_ctype;
 	return size_t_ctype;
@@ -2865,7 +2865,7 @@
 			unrestrict(idx, i_class, &i_type);
 			idx = cast_to(idx, size_t_ctype);
 			m = alloc_const_expression(expr->pos,
-						   ctype->bit_size >> 3);
+						   bits_to_bytes(ctype->bit_size));
 			m->ctype = size_t_ctype;
 			m->flags = Int_const_expr;
 			expr->type = EXPR_BINOP;
diff -rwu ./example.c ../sparse/example.c
--- ./example.c	2008-06-28 22:44:21.706554998 +0100
+++ ../sparse/example.c	2008-06-28 23:47:38.750558346 +0100
@@ -1830,7 +1830,7 @@
 			in->type = REG_FRAME;
 			in->offset = offset;
 			
-			offset += bits >> 3;
+			offset += bits_to_bytes(bits);
 		}
 		i++;
 		NEXT_PTR_LIST(argtype);
diff -rwu ./expand.c ../sparse/expand.c
--- ./expand.c	2008-06-28 22:44:21.710555415 +0100
+++ ../sparse/expand.c	2008-06-28 23:49:46.523309559 +0100
@@ -880,7 +880,7 @@
 {
 	unsigned long offset = 0;
 	while (expr->type == EXPR_POS) {
-		offset += expr->init_offset << 3;
+		offset += bytes_to_bits(expr->init_offset);
 		expr = expr->init_expr;
 	}
 	if (expr && expr->ctype)
diff -rwu ./flow.c ../sparse/flow.c
--- ./flow.c	2008-06-28 22:44:21.710555415 +0100
+++ ../sparse/flow.c	2008-06-28 23:49:45.014554518 +0100
@@ -16,6 +16,7 @@
 #include "expression.h"
 #include "linearize.h"
 #include "flow.h"
+#include "target.h"
 
 unsigned long bb_generation;
 
@@ -265,8 +266,8 @@
 
 static int overlapping_memop(struct instruction *a, struct instruction *b)
 {
-	unsigned int a_start = a->offset << 3;
-	unsigned int b_start = b->offset << 3;
+	unsigned int a_start = bytes_to_bits(a->offset);
+	unsigned int b_start = bytes_to_bits(b->offset);
 	unsigned int a_size = a->size;
 	unsigned int b_size = b->size;
 
@@ -581,13 +582,14 @@
 	pseudo_t pseudo = insn->src;
 
 	if (insn->bb && pseudo->type == PSEUDO_SYM) {
-		int offset = insn->offset, bit = (offset << 3) + insn->size;
+		int offset = insn->offset, bit = bytes_to_bits(offset) + insn->size;
 		struct symbol *sym = pseudo->sym;
 
 		if (sym->bit_size > 0 && (offset < 0 || bit > sym->bit_size))
 			warning(insn->pos, "invalid access %s '%s' (%d %d)",
 				offset < 0 ? "below" : "past the end of",
-				show_ident(sym->ident), offset, sym->bit_size >> 3);
+				show_ident(sym->ident), offset,
+				bits_to_bytes(sym->bit_size));
 	}
 }
 
diff -rwu ./show-parse.c ../sparse/show-parse.c
--- ./show-parse.c	2008-06-28 22:44:21.718555926 +0100
+++ ../sparse/show-parse.c	2008-06-28 23:47:40.010553617 +0100
@@ -673,7 +673,7 @@
 		int new = show_expression(arg);
 		int size = arg->ctype->bit_size;
 		printf("\tpush.%d\t\tv%d\n", size, new);
-		framesize += size >> 3;
+		framesize += bits_to_bytes(size);
 	} END_FOR_EACH_PTR_REVERSE(arg);
 
 	fn = expr->fn;
diff -rwu ./symbol.c ../sparse/symbol.c
--- ./symbol.c	2008-06-28 22:44:21.722555178 +0100
+++ ../sparse/symbol.c	2008-06-28 23:49:45.758555385 +0100
@@ -128,7 +128,7 @@
 		base_size = 0;
 	}
 
-	align_bit_mask = (sym->ctype.alignment << 3) - 1;
+	align_bit_mask = bytes_to_bits(sym->ctype.alignment) - 1;
 
 	/*
 	 * Bitfields have some very special rules..
@@ -143,7 +143,7 @@
 			bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
 			bit_offset = 0;
 		}
-		sym->offset = (bit_size - bit_offset) >> 3;
+		sym->offset = bits_to_bytes(bit_size - bit_offset);
 		sym->bit_offset = bit_offset;
 		sym->ctype.base_type->bit_offset = bit_offset;
 		info->bit_size = bit_size + width;
@@ -156,7 +156,7 @@
 	 * Otherwise, just align it right and add it up..
 	 */
 	bit_size = (bit_size + align_bit_mask) & ~align_bit_mask;
-	sym->offset = bit_size >> 3;
+	sym->offset = bits_to_bytes(bit_size);
 
 	info->bit_size = bit_size + base_size;
 	// warning (sym->pos, "regular: offset=%d", sym->offset);
@@ -182,7 +182,7 @@
 		sym->ctype.alignment = info.max_align;
 	bit_size = info.bit_size;
 	if (info.align_size) {
-		bit_align = (sym->ctype.alignment << 3)-1;
+		bit_align = bytes_to_bits(sym->ctype.alignment)-1;
 		bit_size = (bit_size + bit_align) & ~bit_align;
 	}
 	sym->bit_size = bit_size;
@@ -877,7 +877,7 @@
 		struct symbol *sym = ctype->ptr;
 		unsigned long bit_size = ctype->bit_size ? *ctype->bit_size : -1;
 		unsigned long maxalign = ctype->maxalign ? *ctype->maxalign : 0;
-		unsigned long alignment = (bit_size + 7) >> 3;
+		unsigned long alignment = bits_to_bytes(bit_size + bits_in_char - 1);
 
 		if (alignment > maxalign)
 			alignment = maxalign;
diff -rwu ./target.h ../sparse/target.h
--- ./target.h	2008-06-28 22:44:21.722555178 +0100
+++ ../sparse/target.h	2008-06-28 23:44:50.766554787 +0100
@@ -42,4 +42,14 @@
 extern int bits_in_enum;
 extern int enum_alignment;
 
+/*
+ * Helper functions for converting bits to bytes and vice versa.
+ */
+
+static inline int bits_to_bytes(int bits)
+{ return bits / bits_in_char; }
+
+static inline int bytes_to_bits(int bytes)
+{ return bytes * bits_in_char; }
+
 #endif

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 252 bytes --]

  parent reply	other threads:[~2008-06-29  0:10 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-06-26 23:40 Pointer arithmetic error David Given
2008-06-26 23:51 ` Chris Li
2008-06-27  0:17   ` David Given
2008-06-27  9:00     ` Christopher Li
2008-06-27  9:49     ` Bernd Petrovitsch
2008-06-27 10:55       ` David Given
2008-06-27 11:20         ` Bernd Petrovitsch
2008-06-27 14:03           ` David Given
2008-06-27 14:45             ` Bernd Petrovitsch
2008-06-27 15:45               ` David Given
2008-06-27 18:01                 ` Christopher Li
2008-06-27 23:32                   ` David Given
2008-06-28  0:17                     ` Christopher Li
2008-06-28  0:23                       ` David Given
2008-06-29  0:10                       ` David Given [this message]
2008-06-28  0:29                     ` Josh Triplett
2008-06-29  0:13                     ` Tommy Thorn
     [not found]                     ` <48658B28.6010301@numba-tu.com>
2008-06-29  0:30                       ` David Given
2008-06-29  0:38                         ` Tommy Thorn
2008-06-29 12:19                   ` Bernd Petrovitsch

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=4866D2F4.8090109@cowlark.com \
    --to=dg@cowlark.com \
    --cc=linux-sparse@vger.kernel.org \
    /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 NNTP newsgroup(s).