Trouble with bitfields within arrays

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Trouble with bitfields within arrays

577 Views
ggtours
Contributor I
I have a serious problem with bitfields declared in a structure and manipulating an array of these structures: seems the compiler just ignore the array index when compiling for fast code (what I absolutely require)
I am using CodeWarrior Development Studio for Microcontrollers V6.3 targeting an MCF511C128ACQH
Debugging, looking into the generated assembly code clearly shows this flaw
Below and attached are a listing of the code, an assembly listing which clearly shows the 'i' index stored in 'd0' is ignored to access tstArr.elt[i].flg1 for example: true is set five times at tstArr.elt[0].flg1. I also include contextual setting stuff for the compiler, IDE, tool chain, etc
Is this problem known, what is its extend and what should I avoid? Drop bitfields altogether?
Thank you.
________________
typedef struct 
  {
    unsigned short us1, us2;
    unsigned char flg1:1;
    unsigned char flg2:1;
  }sElt;

typedef struct 
  {
    unsigned short size;
    sElt elt[10];
  }sArrOfElt;

sArrOfElt tstArr;

void initElt(void)
  {
   unsigned short i;

   tstArr.size = 5;
   for(i=0; i<tstArr.size; i++)
    {
     tstArr.elt[i].flg1 = 1;
     tstArr.elt[i].flg2 = 0;
     tstArr.elt[i].us1 = i;
     tstArr.elt[i].us2 = i<<1;
    }
  }
__________________
Producer:             CodeWarrior ColdFire C/C++ Compiler

;
;  184:  
;  185: sArrOfElt tstArr; 
;  186:  
;  187: void initElt(void) 
;  188:   { 
;  189:    unsigned short i; 
;  190:    
;  191:    tstArr.size = 5; 
;  192:       for(
;
0x00002810                    _initElt:
;                             initElt:
0x00002810  0x3B7C00052580           move.w   #5,_tstArr            ; '..'
;
;  192:        i=0; 
;
0x00002816  0x4240                   clr.w    d0
0x00002818  0x603C                   bra.s    *+62                  ; 0x00002856
;
;  194:      tstArr.elt[i].flg1 = 1; 
;
0x0000281A  0x41ED2586               lea      _tstArr+6(a5),a0
0x0000281E  0x4201                   clr.b    d1
0x00002820  0x03F01A00               bset     d1,(a0,d1.l*2)
;
;  195:      tstArr.elt[i].flg2 = 0; 
;
0x00002824  0x41ED2586               lea      _tstArr+6(a5),a0
0x00002828  0xA341                   mov3q    #1,d1
0x0000282A  0x03B01A00               bclr     d1,(a0,d1.l*2)
;
;  196:      tstArr.elt[i].us1 = i; 
;
0x0000282E  0x75C0                   mvz.w    d0,d2
0x00002830  0x2202                   move.l   d2,d1
0x00002832  0xE389                   lsl.l    #1,d1
0x00002834  0xD282                   add.l    d2,d1
0x00002836  0x41ED2582               lea      _tstArr+2(a5),a0
0x0000283A  0x31801A00               move.w   d0,(a0,d1.l*2)
;
;  197:      tstArr.elt[i].us2 = i<<1; 
;  198:          } 
;
0x0000283E  0x73C0                   mvz.w    d0,d1
0x00002840  0x2401                   move.l   d1,d2
0x00002842  0xE38A                   lsl.l    #1,d2
0x00002844  0xD481                   add.l    d1,d2
0x00002846  0x41ED2584               lea      _tstArr+4(a5),a0
0x0000284A  0x73C0                   mvz.w    d0,d1
0x0000284C  0xE389                   lsl.l    #1,d1
0x0000284E  0x31812A00               move.w   d1,(a0,d2.l*2)
;
;  192:                            i++) 
;  193:                                { 
;
0x00002852  0x71C0                   mvz.w    d0,d0
0x00002854  0x5280                   addq.l   #1,d0
;
;  192:             i<tstArr.size; 
;
0x00002856  0xB06D2580               cmp.w    _tstArr,d0
0x0000285A  0x65BE                   bcs.s    *-64                  ; 0x0000281a
;
;  199:   } 
;
0x0000285C  0x4E75                   rts      
0x0000285E  0x51FC                   trapf    
;
;  200:  
;
_______________
Installed Products:
CodeWarrior Development Studio for Microcontrollers V6.3, build 9279.
IDE Version: 5.9.0 Build 5292
asm_mcf_elf.dll           6.0 build 38              5.0.0.38  
 ELFImporter.dll           1.0 Build 0005            1.0.0.5
 MCFCCompiler.dll       6.0 build 38              6.0.0.38  
 OsekCompiler.dll         2, 1, 2, 0                    
 PCLintCompiler.dll       1.5                       1.5.0.5 
 MCFLinker.dll             6.0 build 38 
 PCLintLinker.dll         1.5                       1.5.0.5
Labels (1)
0 Kudos
1 Reply

336 Views
ggtours
Contributor I

Some more contextual stuff in attachements. Sorry to say this but they are so many limitations and strange things with this message editor that posting is an absolute time sink...  :smileyhappy:

0 Kudos