🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.bug
Article #6016

index value truncated to 16 bits

#6016
From: Michel Delval
Date: Thu, 02 Jan 1997 00:00
123 lines
4742 bytes

	Hi !

	I am using gcc as a cross-compiler sunos-4.1 -> 16bits
architecture and I've got a problem with the following code:

----------------

int tab[8192];

proc()
{
  tab[4096] = 1;
}

----------------

	The produced assembler code is the same as if the source code
was ``tab[0] = 1;'' instead of ``tab[4096] = 1;''.

	Idem for ``tab[4097] = 1;'' : asm <=> ``tab[1] = 1;''
	Idem for ``tab[4098] = 1;'' : asm <=> ``tab[2] = 1;''
	...

	When the C code is changed as follow, resulted assembler code
is correct :

----------------

int tab[8192];

proc()
{
  int i = 4096;
  tab[i] = 1;
}

----------------

	When I debug GCC it appears that const_binop() is called with
the following arguments : MULT_EXPR, 16 and 4096, through
get_inner_reference() (ARRAY_REF case) for computing a bit offset; and
force_fit_type() is called before const_binop() returns, truncating
``4096 * 16 = 0x10000'' to 16-bits value 0x0.

	I was able to reproduce the same computation path with
gcc-2.7.2.1 compiled for sunos-4.1; of course, the bug didn't appear
because 0x10000 fits in 32 bits. You will find the gdb session at the
end of this mail.

	I can understand that the bit-offset of an assignment is of
integer type and can be folded to fit the size of an integer; but in
my case, the target integer size is 16 bits wide. So what can I do ?
Is this really a bug ?

	For the purpose of my gcc port, I made a kludge in
const_binop(), after label ``got_it:'' because I wasn't able to find
some real good place to patch.

----------------
    got_it:
      if (code == MULT_EXPR)
	notrunc = 1;
----------------

	Thanks in advance for any help/hints.



			[mfd]

	+-------------------------------------------------------+
	|  Michel DELVAL					|
	+-------------------------------------------------------+
	|  E-mail:	mfd@ccv.fr				|
	+-------------------------------------------------------+
	|  Computers, Communications				|
	|  and Visions (C2V)       				|
	|  82 boulevard Haussmann				|
	|  75008 Paris FRANCE					|
	|  Phone	(1) 40 08 07 07				|
	|  Fax		(1) 43 87 35 99				|
	+-------------------------------------------------------+
	|	Hackers do it with bugs.			|
	+-------------------------------------------------------+


----------------------------------------------------------------

(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /files2/tmp/gcc-2.7.2.1/cc1 -quiet -O1 /tmp/b86.c -dumpbase /tmp/b86 

Breakpoint 2, const_binop (code=MULT_EXPR, arg1=0x1936f8, arg2=0x194a60, notrunc=0) at fold-const.c:1067
(gdb) p arg1->int_cst
$57 = {common = "\000\000\000\000\000\031\201 \031`\000", int_cst_low = 32, int_cst_high = 0}
(gdb) p arg2->int_cst
$58 = {common = "\000\000\000\000\000\0316p\031@\000", int_cst_low = 4096, int_cst_high = 0}
(gdb) 
(gdb) bt
#0  const_binop (code=MULT_EXPR, arg1=0x1936f8, arg2=0x194a60, notrunc=0) at fold-const.c:1067
#1  0x46c28 in fold (expr=0x194ac0) at fold-const.c:3794
#2  0x5f164 in get_inner_reference (exp=0x194a78, pbitsize=0xefffec84, pbitpos=0xefffec80, poffset=0xefffec7c, pmode=0xefffec78, punsignedp=0x194a60, 
    pvolatilep=0xefffec70) at expr.c:3630
#3  0x5d228 in expand_assignment (to=0x194a78, from=0x194a90, want_value=0, suggest_reg=1) at expr.c:2500
#4  0x64530 in expand_expr (exp=0x194aa8, target=0x0, tmode=VOIDmode, modifier=EXPAND_NORMAL) at expr.c:6241
#5  0x53638 in expand_expr_stmt (exp=0x194aa8) at stmt.c:1724
#6  0x2e7e8 in expand_stmt_with_iterators_1 (stmt=0x194aa8, iter_list=0x0) at c-iterate.c:164
#7  0x2e7c0 in iterator_expand (stmt=0x194aa8) at c-iterate.c:154
#8  0xa340 in yyparse () at c-parse.y:1561
#9  0x3210c in compile_file (name=0xeffff670 "/tmp/b86.c") at toplev.c:2222
#10 0x35220 in main (argc=6, argv=0xeffff534, envp=0xeffff550) at toplev.c:3908
(gdb) i b
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0xef7bf42c  <abort+4>
2   breakpoint     keep y   0x00041a38 in const_binop at fold-const.c:1067
	stop only if arg1->int_cst.int_cst_low == 32 && arg2->int_cst.int_cst_low == 4096
	breakpoint already hit 5 times
(gdb)

----------------------------------------------------------------
Technical Details
Message-ID: <199701021656.QAA02067@localhost>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113d02,25f2bc9153f6751f,start
X-Google-Attributes: gid113d02,public
From: mfd@ccv.fr (Michel Delval)
Subject: index value truncated to 16 bits
Date: 1997/01/02
Message-ID: <199701021656.QAA02067@localhost>
X-Deja-AN: 207290192
sender: bug-gcc-request@prep.ai.mit.edu
x-gateway: relay3.UU.NET from bug-gcc to gnu.gcc.bug; Thu, 2 Jan 1997 11:22:20 EST
newsgroups: gnu.gcc.bug