From: Scott B. <br...@ri...> - 2003-07-29 00:08:48
Attachments:
structtest2.tar.gz
|
I think I've found a bug in SDCC, but because I'm not real confident of my understanding of pointers and 8051 memory areas, I'm not sure. Here's the deal: If I define a structure in the C file in which it is used, weverything works perfectly. However, if I define the structure in another C file ("extern DeviceDescriptor dd;"), it goes horribly wrong. I get garbage when I read from the structure (the simulator always returns 0, and my 8051 dev board returns values from code memory). Here's how to reproduce this using the attached code. 1. Verify that INCLUDE_DD is set to 1 in dd.h 2. "make sim", and in another window "make net" 3. Type "go" in the simulator window. Notice how the serial output is correct: "dd.length: 42" 4. Set INCLUDE_DD to 0 in dd.h. This means that the dd struct will be defined in dd.c rather than test.c. 5. "make sim", "make net" 6. "go". Serial output: "dd.length: 0" Can anyone tell me what's going wrong? Thanks, - Scott |
From: Josh S. <jo...@4s...> - 2003-07-29 15:22:22
|
Your 'dd' declaration is being created in the code segment. This is because it is a constant. The issue here is that the asm for test.c is created before it knows where dd is stored. Your extern statement does not specify that dd is a constant, so test.asm is created assuming that dd will be in the idata section. If you add a const qualifier on your extern, it should work fine. I didn't try it myself, but the generated code is now correct... Josh Stone > -----Original Message----- > From: sdc...@li... > [mailto:sdc...@li...]On Behalf Of Scott Bronson > Sent: Monday, July 28, 2003 6:20 PM > To: sdc...@li... > Subject: [Sdcc-user] Help chasing a struct bug? > > > I think I've found a bug in SDCC, but because I'm not > real confident of my understanding of pointers and 8051 > memory areas, I'm not sure. > > Here's the deal: If I define a structure in the C file > in which it is used, weverything works perfectly. However, > if I define the structure in another C file > ("extern DeviceDescriptor dd;"), it goes horribly wrong. > I get garbage when I read from the structure (the simulator > always returns 0, and my 8051 dev board returns values from > code memory). > > > Here's how to reproduce this using the attached code. > > 1. Verify that INCLUDE_DD is set to 1 in dd.h > 2. "make sim", and in another window "make net" > 3. Type "go" in the simulator window. Notice how > the serial output is correct: "dd.length: 42" > > 4. Set INCLUDE_DD to 0 in dd.h. This means that > the dd struct will be defined in dd.c rather > than test.c. > 5. "make sim", "make net" > 6. "go". Serial output: "dd.length: 0" > > Can anyone tell me what's going wrong? Thanks, > > - Scott > > |
From: Scott B. <br...@ri...> - 2003-07-29 20:42:45
|
Well, that does work! Nice. Thanks, Josh. SDCC doesn't appear to warn you if your extern declaration doesn't match your structure definition with regards to const, and generates bad code as a result. I don't see any open bugs about this. Should I open a bug? Here are some ways to reproduce it: typedef struct { unsigned char a; } A; extern A a; const A a = { 1 }; void main() {} struct B { unsigned char a; }; extern struct B b; const struct B b = { 1 }; void main() {} On Tue, 2003-07-29 at 07:37, Josh Stone wrote: > Your 'dd' declaration is being created in the code segment. This is because > it is a constant. The issue here is that the asm for test.c is created > before it knows where dd is stored. Your extern statement does not specify > that dd is a constant, so test.asm is created assuming that dd will be in > the idata section. If you add a const qualifier on your extern, it should > work fine. I didn't try it myself, but the generated code is now correct... > > > Josh Stone > > > > -----Original Message----- > > From: sdc...@li... > > [mailto:sdc...@li...]On Behalf Of Scott Bronson > > Sent: Monday, July 28, 2003 6:20 PM > > To: sdc...@li... > > Subject: [Sdcc-user] Help chasing a struct bug? > > > > > > I think I've found a bug in SDCC, but because I'm not > > real confident of my understanding of pointers and 8051 > > memory areas, I'm not sure. > > > > Here's the deal: If I define a structure in the C file > > in which it is used, weverything works perfectly. However, > > if I define the structure in another C file > > ("extern DeviceDescriptor dd;"), it goes horribly wrong. > > I get garbage when I read from the structure (the simulator > > always returns 0, and my 8051 dev board returns values from > > code memory). > > > > > > Here's how to reproduce this using the attached code. > > > > 1. Verify that INCLUDE_DD is set to 1 in dd.h > > 2. "make sim", and in another window "make net" > > 3. Type "go" in the simulator window. Notice how > > the serial output is correct: "dd.length: 42" > > > > 4. Set INCLUDE_DD to 0 in dd.h. This means that > > the dd struct will be defined in dd.c rather > > than test.c. > > 5. "make sim", "make net" > > 6. "go". Serial output: "dd.length: 0" > > > > Can anyone tell me what's going wrong? Thanks, > > > > - Scott > > > > > > > > ------------------------------------------------------- > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > Data Reports, E-commerce, Portals, and Forums are available now. > Download today and enter to win an XBOX or Visual Studio .NET. > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 > _______________________________________________ > Sdcc-user mailing list > Sdc...@li... > https://lists.sourceforge.net/lists/listinfo/sdcc-user > |
From: Josh S. <jo...@4s...> - 2003-07-29 21:06:58
|
I agree, this would be a very useful (and perhaps necessary) warning... > -----Original Message----- > From: sdc...@li... > [mailto:sdc...@li...]On Behalf Of Scott Bronson > Sent: Tuesday, July 29, 2003 2:53 PM > To: sdc...@li... > Subject: RE: [Sdcc-user] Help chasing a struct bug? > > > Well, that does work! Nice. Thanks, Josh. > > SDCC doesn't appear to warn you if your extern declaration doesn't > match your structure definition with regards to const, and generates > bad code as a result. I don't see any open bugs about this. Should > I open a bug? > > Here are some ways to reproduce it: > > > > typedef struct { > unsigned char a; > } A; > > extern A a; > > const A a = { 1 }; > > void main() {} > > > > > > struct B { > unsigned char a; > }; > > extern struct B b; > > const struct B b = { 1 }; > > void main() {} > > > > > On Tue, 2003-07-29 at 07:37, Josh Stone wrote: > > Your 'dd' declaration is being created in the code segment. > This is because > > it is a constant. The issue here is that the asm for test.c is created > > before it knows where dd is stored. Your extern statement does > not specify > > that dd is a constant, so test.asm is created assuming that dd > will be in > > the idata section. If you add a const qualifier on your > extern, it should > > work fine. I didn't try it myself, but the generated code is > now correct... > > > > > > Josh Stone > > > > > > > -----Original Message----- > > > From: sdc...@li... > > > [mailto:sdc...@li...]On Behalf Of > Scott Bronson > > > Sent: Monday, July 28, 2003 6:20 PM > > > To: sdc...@li... > > > Subject: [Sdcc-user] Help chasing a struct bug? > > > > > > > > > I think I've found a bug in SDCC, but because I'm not > > > real confident of my understanding of pointers and 8051 > > > memory areas, I'm not sure. > > > > > > Here's the deal: If I define a structure in the C file > > > in which it is used, weverything works perfectly. However, > > > if I define the structure in another C file > > > ("extern DeviceDescriptor dd;"), it goes horribly wrong. > > > I get garbage when I read from the structure (the simulator > > > always returns 0, and my 8051 dev board returns values from > > > code memory). > > > > > > > > > Here's how to reproduce this using the attached code. > > > > > > 1. Verify that INCLUDE_DD is set to 1 in dd.h > > > 2. "make sim", and in another window "make net" > > > 3. Type "go" in the simulator window. Notice how > > > the serial output is correct: "dd.length: 42" > > > > > > 4. Set INCLUDE_DD to 0 in dd.h. This means that > > > the dd struct will be defined in dd.c rather > > > than test.c. > > > 5. "make sim", "make net" > > > 6. "go". Serial output: "dd.length: 0" > > > > > > Can anyone tell me what's going wrong? Thanks, > > > > > > - Scott > > > > > > > > > > > > > > ------------------------------------------------------- > > This SF.Net email sponsored by: Free pre-built ASP.NET sites including > > Data Reports, E-commerce, Portals, and Forums are available now. > > Download today and enter to win an XBOX or Visual Studio .NET. > > http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 > _______________________________________________ > Sdcc-user mailing list > Sdc...@li... > https://lists.sourceforge.net/lists/listinfo/sdcc-user > ------------------------------------------------------- This SF.Net email sponsored by: Free pre-built ASP.NET sites including Data Reports, E-commerce, Portals, and Forums are available now. Download today and enter to win an XBOX or Visual Studio .NET. http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01/01 _______________________________________________ Sdcc-user mailing list Sdc...@li... https://lists.sourceforge.net/lists/listinfo/sdcc-user |