9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] c compiler bug
@ 2010-09-14 15:25 erik quanstrom
  2010-09-14 20:20 ` Charles Forsyth
  0 siblings, 1 reply; 27+ messages in thread
From: erik quanstrom @ 2010-09-14 15:25 UTC (permalink / raw)
  To: 9fans

it appears that ?c do not properly do structure
assignment if the structure is packed.

i've included a patch just for 8c and a sample
program that generates two "packed assignment"
warnings with the modified compiler.

feedback appreciated unless it's "don't do that."

if this looks entirely reasonable, i'll submit a patch
for all the compilers.

- erik

---

; diffy -c /sys/src/cmd/8c/cgen.c
/n/dump/2010/0914/sys/src/cmd/8c/cgen.c:1824,1829 - /sys/src/cmd/8c/cgen.c:1824,1836
  	gins(ACLD, Z, Z);
  	gins(AREP, Z, Z);
  	gins(AMOVSL, Z, Z);
+ 	if(x = w & SZ_LONG-1){
+ 		warn(n, "packed assignment");
+ 		gins(AMOVL, nodconst(x), &nod3);
+ 	//	gins(ACLD, Z, Z);
+ 		gins(AREP, Z, Z);
+ 		gins(AMOVSB, Z, Z);
+ 	}
  	if(c & 4) {
  		gins(APOPL, Z, &nod3);
  		reg[D_CX]--;

---

#include <u.h>
#include <libc.h>

#pragma pack on
typedef struct S S;
struct	S
{
	uchar	a;
	ushort	b;
	uint	c;
};
#pragma pack off

S
func(void)
{
	S s;

	s.a = 1;
	s.b = 2;
	s.c = 3;
	return s;
}

void
main(void)
{
	S s, t;

	memset(&s, 0xff, sizeof s);
	print("sizeof s = %d\n", sizeof s);
	s = func();
	memset(&t, 0xff, sizeof t);
	t = s;
	print("a=%d b=%d c=%d\n", t.a, t.b, t.c);
	exits(nil);
}



^ permalink raw reply	[flat|nested] 27+ messages in thread
* [9fans] c compiler bug
@ 2013-02-17  2:38 erik quanstrom
  2013-02-18 13:02 ` Comeau At9Fans
  0 siblings, 1 reply; 27+ messages in thread
From: erik quanstrom @ 2013-02-17  2:38 UTC (permalink / raw)
  To: 9fans

i don't think this should link, since wrongaddr calls
fn with an Outer* not an Inner*.

#include <u.h>
#include <libc.h>

typedef	struct	Inner	Inner;
typedef	struct	Outer	Outer;

struct Inner {
	int	x;
};

struct Outer {
	char	buf[0x1000];
	Inner;
};

void
wrongaddr(Outer *o)
{
	static void fn(Outer*);

	fn(o);
}

void
main(void)
{
	Outer *o;

	o = malloc(sizeof *o);
	memset(o, 0, sizeof *o);
	print("addr o	%#p\n", o);
	print("addr o.x	%#p\n", &o->x);
	wrongaddr(o);
}

static void
fn(Inner *i)
{
	print("fn addr i.x	%#p\n", &i->x);
}
; 6.cbug
addr o	0x4018f0
addr o.x	0x4028f0
fn addr i.x	0x4018f0



^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2013-02-21 20:36 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-09-14 15:25 [9fans] c compiler bug erik quanstrom
2010-09-14 20:20 ` Charles Forsyth
2010-09-14 20:59   ` Brantley Coile
2010-09-14 21:07     ` ron minnich
2010-09-15  7:06       ` Sape Mullender
2010-09-15  8:30         ` Charles Forsyth
2010-09-15  8:37         ` Charles Forsyth
2010-09-15 12:20           ` Charles Forsyth
2010-09-15  8:32       ` Charles Forsyth
2013-02-17  2:38 erik quanstrom
2013-02-18 13:02 ` Comeau At9Fans
2013-02-18 14:38   ` Charles Forsyth
2013-02-18 15:02     ` erik quanstrom
2013-02-18 15:10       ` Charles Forsyth
2013-02-21 17:17       ` Comeau At9Fans
2013-02-21 17:13     ` Comeau At9Fans
2013-02-21 18:13       ` hiro
2013-02-21 18:22         ` John Floren
2013-02-21 18:39           ` erik quanstrom
2013-02-21 18:46             ` Comeau At9Fans
2013-02-21 18:51               ` John Floren
2013-02-21 19:36                 ` hiro
2013-02-21 19:58                   ` andrey mirtchovski
2013-02-21 20:24                     ` Matthew Veety
2013-02-21 20:27                     ` David Leimbach
2013-02-21 20:36                       ` steve
2013-02-21 18:51             ` Kurt H Maier

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).