That is disappointing.  OK.  How about this, then?

Peter
llc[58]$ cat /tmp/x.c
#define DIRTY_FLAG 0x4
volatile int state;

#define _ATOMIC_BIC(_dst,_src) do {                                     \
    __asm__ __volatile__ ("bic\t%0, %1"                                 \
                          : "=m" (_dst)                                 \
                          : "g" (_src));                                \
  } while (0)
#define _ATOMIC_BIS(_dst,_src) do {                                     \
    __asm__ __volatile__ ("bis\t%0, %1"                                 \
                          : "=m" (_dst)                                 \
                          : "g" (_src));                                \
  } while (0)

void doit (int x) {
  if (x) {
    _ATOMIC_BIS(state, (state >> 1) & DIRTY_FLAG);
  } else {
    _ATOMIC_BIC(state, (state << 1) & ~DIRTY_FLAG);
  }
}
llc[59]$ /msp430/install/lts-20120406-20120502/bin/msp430-gcc -Os -S /tmp/x.c
llc[60]$ cat x.s
        .file   "x.c"
        .cpu 430
        .mpy none

        .text
        .p2align 1,0
.global doit
        .type   doit,@function
/***********************
 * Function `doit'
 ***********************/
doit:
        cmp     #0, r15
        jeq     .L2
        mov     &state, r15
        rra     r15
        and     #4, r15
#APP
 ;  17 "/tmp/x.c" 1
        bis     &state, r15
 ;  0 "" 2
#NOAPP
        ret
.L2:
        mov     &state, r15
        rla     r15
        and     #llo(-5), r15
#APP
 ;  19 "/tmp/x.c" 1
        bic     &state, r15
 ;  0 "" 2
#NOAPP
        ret
.Lfe1:
        .size   doit,.Lfe1-doit
;; End of function

        .comm state,2,2
llc[61]$

On Sun, Sep 2, 2012 at 3:21 PM, William Swanson <swanson...@gmail.com> wrote:
> On Sun, Sep 2, 2012 at 1:02 PM, Peter Bigot <big...@acm.org> wrote:
>> 1) The solution mentioned before and made explicit below, which is not
>> optimal but does appear to meet your stated requirements for
>> atomicity;
>
> Right, I have been investigating that solution, and it's not 100%
> reliable. Depending on what the branch structure looks like, the
> compiler can sometimes insert a jump which destroys the peephole
> optimization. For example:
>
>     if (blah)
>       flags |= some_bit;
>     else
>       flags &= ~some_bit;
>
> This sometimes compiles to:
>
>     TST.B   blah
>     MOV.B   &flags, R14
>     JZ      else
>     BIS.B   #0x0008, R14
>     JMP     skip
> else:
>     AND.B   #0x00f7, R14
> skip:
>     MOV.B   R14,     &flags
>
> I can't just "set it and forget it" with option 1; I have to verify
> that the assembly matches my intent, and re-verify it each time I
> change something. Option 3 would be more work than option 2, but would
> solve this problem once and for all for everyone.
>
> -William

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users

Reply via email to