gcc extended inline asm, 'cc' clobber

gcc extended inline asm, 'cc' clobber

Postby notzed » Sun Jul 06, 2014 10:28 am

How does one specify that inline asm will clobber the condition codes? I've tried this:

Code: Select all
unsigned int bitrev(unsigned int val) {
   unsigned int res;

   asm ("bitr %[res],%[val]"
        : [res] "=r" (res)
        : [val] "r" (val)
        : "cc");

   return res;
}


But it doesn't work. Sometimes the scheduler interleaves it with conditional code such as this:

Code: Select all
 600:   90af 908a       lsl r36,r36,r9
 604:   481f 900e       bitr r34,r34
 608:   8bbb 307f       sub r12,r34,1023
 60c:   001f 900e       bitr r32,r32
 610:   b41f 900e       bitr r37,r37
 614:   901f 900e       bitr r36,r36
 618:   a890            blte 568 <_forward_1024+0x5c>


Code: Select all
$ e-gcc --version
e-gcc (Epiphany toolchain (built 20130910)) 4.8.2 20130729 (prerelease)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


I don't normally use inline asm so i'm not that familiar with it - is this a bug or just luser error?

As a workaround (?) "asm volatile()" seems to at least product correctly-executing code (for this case at least).
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia

Re: gcc extended inline asm, 'cc' clobber

Postby aolofsson » Wed Aug 20, 2014 10:19 pm

notzed,
Sorry for the slow response! We have mostly used __volatile__ in the past.
Not sure if there is another way?
Andreas
User avatar
aolofsson
 
Posts: 1005
Joined: Tue Dec 11, 2012 6:59 pm
Location: Lexington, Massachusetts,USA

Re: gcc extended inline asm, 'cc' clobber

Postby notzed » Wed Aug 20, 2014 11:14 pm

aolofsson wrote:notzed,
Sorry for the slow response! We have mostly used __volatile__ in the past.
Not sure if there is another way?
Andreas


No worries.

My reading of the gcc manual is that the mechanism is to use the "cc" flag added to the asm statement as a 3rd parameter. It is used for ISAs that use condition codes to indicate that they are clobbered by the instruction. I guess it just hasn't been implemented for epiphany since most instructions clobber them, or i'm misreading the manual.

If your assembler instruction can alter the condition code register,
add `cc' to the list of clobbered registers. GCC on some machines
represents the condition codes as a specific hardware register; `cc'
serves to name this register. On other machines, the condition code is
handled differently, and specifying `cc' has no effect. But it is
valid no matter what the machine.


Whilst volatile produces functional code it leads to suboptimal output, but I guess my original concern was that not using volatile will often produce non-functional code; which seems unexpected.
notzed
 
Posts: 331
Joined: Mon Dec 17, 2012 12:28 am
Location: Australia


Return to Assembly

Who is online

Users browsing this forum: No registered users and 3 guests