I think I've run into a compiler bug, but I'm not sure how to pin it
down.

I'm using the TinyOS RandomMlcg random generator:

  /* Return the next 32 bit random number */
  async command uint32_t Random.rand32() {
    uint32_t mlcg,p,q;
    uint64_t tmpseed;
    atomic
      {
        tmpseed =  (uint64_t)33614U * (uint64_t)seed;
        q = tmpseed;    /* low */
        q = q >> 1;
        p = tmpseed >> 32 ;             /* hi */
        mlcg = p + q;
        if (mlcg & 0x80000000) { 
          mlcg = mlcg & 0x7FFFFFFF;
          mlcg++;
        }
        seed = mlcg;
      }
    return mlcg; 
  }

Now, in my program, I have a function that takes one argument. After the
call to Random.rand16(), this argument is overwritten by the value
0x834e - which is 33614U. This happens with mspgcc4 (20110813) but not
with 3.2.3. My trivial test program failed to reproduce this; I'll see
if I can find a way to trigger it.

The context in which it's called (noinlined so I can find it in the
disassembly):

        void backoff(uint16_t fixed) __attribute__((noinline)) {
                uint16_t delay = 0;
                printf("fixed=%u\n", fixed);    // here, fixed is
correct
                delay = call Random.rand16();
                printf("fixed=%u\n", fixed);    // here, fixed == 0x834e
                ...

For reference, the disassembly of the working version (3.2.3) followed
by the broken one, up to the second call to printf:

3.2.3:
00005204 <SendP__backoff>:
    5204:       1b 15           .word   0x151b; ????    
    5206:       0a 4f           mov     r15,    r10     
    5208:       0f 12           push    r15             
    520a:       30 12 fa 51     push    #20986          ;#0x51fa
    520e:       b0 13           .word   0x13b0; ????    
    5210:       6a 7d           subc.b  @r13,   r10     
    5212:       21 52           add     #4,     r1      ;r2 As==10
    5214:       b0 13           .word   0x13b0; ????    
    5216:       5e 52 0b 4e     add.b   &0x4e0b,r14     
    521a:       0a 12           push    r10             
    521c:       30 12 fa 51     push    #20986          ;#0x51fa
    5220:       b0 13           .word   0x13b0; ????    
    5222:       6a 7d           subc.b  @r13,   r10     
    5224:       21 52           add     #4,     r1      ;r2 As==10
    5226:       0a 4b           mov     r11,    r10     
    5228:       0b 43           clr     r11             
    522a:       b0 13           .word   0x13b0; ????    
    522c:       f6 34           jge     $+494           ;abs 0x541a
    522e:       53 12           push.b  #1              ;r3 As==01
    5230:       0b 12           push    r11             
    5232:       0a 12           push    r10             
    5234:       0d 4e           mov     r14,    r13     
    5236:       0e 4f           mov     r15,    r14     
    5238:       7f 40 03 00     mov.b   #3,     r15     ;#0x0003
    523c:       b0 13           .word   0x13b0; ????    
    523e:       4a 42           mov.b   r2,     r10     
    5240:       31 50 06 00     add     #6,     r1      ;#0x0006
    5244:       1f 42 70 14     mov     &0x1470,r15     
    5248:       0f 5f           rla     r15             
    524a:       82 4f 70 14     mov     r15,    &0x1470 
    524e:       3f 90 11 00     cmp     #17,    r15     ;#0x0011
    5252:       03 28           jnc     $+8             ;abs 0x525a
    5254:       b2 40 10 00     mov     #16,    &0x1470 ;#0x0010
    5258:       70 14 
    525a:       1a 17           .word   0x171a; ????    
    525c:       10 01           .word   0x0110; ????    

0000525e <RandomMlcgC__Random__rand32>:
    525e:       4b 15           .word   0x154b; ????    
    5260:       b0 13           .word   0x13b0; ????    
    5262:       8a 31           jn      $+790           ;abs 0x5578
    5264:       47 4f           mov.b   r15,    r7      
    5266:       1c 42 c8 13     mov     &0x13c8,r12     
    526a:       1d 42 ca 13     mov     &0x13ca,r13     
    526e:       0e 43           clr     r14             
    5270:       0f 43           clr     r15             
    5272:       03 12           push    #0              ;r3 As==00
    5274:       03 12           push    #0              ;r3 As==00
    5276:       03 12           push    #0              ;r3 As==00
    5278:       30 12 4e 83     push    #-31922 ;#0x834e
    527c:       b0 13           .word   0x13b0; ????    
    527e:       f6 86 31 52     sub.b   @r6+,   21041(r6);0x5231(r6)
    5282:       0a 4c           mov     r12,    r10     
    5284:       0b 4d           mov     r13,    r11     
    5286:       12 c3           clrc                    
    5288:       0b 10           rrc     r11             
    528a:       0a 10           rrc     r10             
    528c:       0c 4e           mov     r14,    r12     
    528e:       0d 4f           mov     r15,    r13     
    5290:       0e 43           clr     r14             
    5292:       0f 43           clr     r15             
    5294:       08 4c           mov     r12,    r8      
    5296:       09 4d           mov     r13,    r9      
    5298:       08 5a           add     r10,    r8      
    529a:       09 6b           addc    r11,    r9      
    529c:       39 b0 00 80     bit     #-32768,r9      ;#0x8000
    52a0:       04 24           jz      $+10            ;abs 0x52aa
    52a2:       39 f0 ff 7f     and     #32767, r9      ;#0x7fff
    52a6:       18 53           inc     r8              
    52a8:       09 63           adc     r9              
    52aa:       82 48 c8 13     mov     r8,     &0x13c8 
    52ae:       82 49 ca 13     mov     r9,     &0x13ca 
    52b2:       4f 47           mov.b   r7,     r15     
    52b4:       b0 13           .word   0x13b0; ????    
    52b6:       9e 31           jn      $+830           ;abs 0x55f4
    52b8:       0e 48           mov     r8,     r14     
    52ba:       0f 49           mov     r9,     r15     
    52bc:       47 17           .word   0x1747; ????    
    52be:       10 01           .word   0x0110; ????    


20110813 inlines the call:

00004c90 <SendP__backoff>:
    4c90:       0b 12           push    r11             
    4c92:       0a 12           push    r10             
    4c94:       09 12           push    r9              
    4c96:       08 12           push    r8              
    4c98:       07 12           push    r7              
    4c9a:       06 12           push    r6              
    4c9c:       05 12           push    r5              
    4c9e:       04 12           push    r4              
    4ca0:       21 83           decd    r1              
    4ca2:       0f 12           push    r15             
    4ca4:       0f 12           push    r15             
    4ca6:       30 12 80 4c     push    #19584          ;#0x4c80
    4caa:       b0 12 b8 76     call    #0x76b8 
    4cae:       31 50 06 00     add     #6,     r1      ;#0x0006
    4cb2:       b0 12 06 3a     call    #0x3a06 
    4cb6:       c1 4f 00 00     mov.b   r15,    0(r1)   ;0x0000(r1)
    4cba:       18 42 cc 13     mov     &0x13cc,r8      
    4cbe:       19 42 ce 13     mov     &0x13ce,r9      
    4cc2:       34 40 4e 83     mov     #-31922,r4      ;#0x834e
    4cc6:       05 43           clr     r5              
    4cc8:       b0 12 36 7c     call    #0x7c36 
    4ccc:       0a 4c           mov     r12,    r10     
    4cce:       0b 4d           mov     r13,    r11     
    4cd0:       12 c3           clrc                    
    4cd2:       0b 10           rrc     r11             
    4cd4:       0a 10           rrc     r10             
    4cd6:       0a 5e           add     r14,    r10     
    4cd8:       0b 6f           addc    r15,    r11     
    4cda:       0b 93           tst     r11             
    4cdc:       05 34           jge     $+12            ;abs 0x4ce8
    4cde:       3a f3           and     #-1,    r10     ;r3 As==11
    4ce0:       3b f0 ff 7f     and     #32767, r11     ;#0x7fff
    4ce4:       1a 53           inc     r10             
    4ce6:       0b 63           adc     r11             
    4ce8:       82 4a cc 13     mov     r10,    &0x13cc 
    4cec:       82 4b ce 13     mov     r11,    &0x13ce 
    4cf0:       6f 41           mov.b   @r1,    r15     
    4cf2:       b0 12 1a 3a     call    #0x3a1a 
    4cf6:       04 12           push    r4              
    4cf8:       04 12           push    r4              
    4cfa:       30 12 80 4c     push    #19584          ;#0x4c80
    4cfe:       b0 12 b8 76     call    #0x76b8

I'm afraid my assembly-fu isn't very strong, but I hope someone can
glean... something from it.

Best,
Michiel


------------------------------------------------------------------------------
BlackBerry&reg; DevCon Americas, Oct. 18-20, San Francisco, CA
Learn about the latest advances in developing for the 
BlackBerry&reg; mobile platform with sessions, labs & more.
See new tools and technologies. Register for BlackBerry&reg; DevCon today!
http://p.sf.net/sfu/rim-devcon-copy1 
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to