knowing how a compiler works in the most unusual situations is always
interesting...and this discussion is no different.
however, a compiler is just a machine with a lot of rules, so you
cannot expect it to be "clever" anytime, even if correct code is a
necessity and efficiency is only a plus;
but in this case, reading the C source code, I honestly can't
understand for sure what it meant....if I were to translate manually
that code into asm, I would have asked a clarification to the
programmer: did you want to sample twice the port and then add these
two samples ? (say for making an average) or did you want two sample
the port once and double that reading ?
IMHO the answer wasn't clear, even if I considered all the volatile qualifiers;
so I wouldn't try to add another complicated rule to gcc, but I would
rather write a better C code...an unambiguous one that the "simple"
rules of any compiler can understand.

for sampling twice:  x = P6IN;   x += P6IN;
for doubling the sampling:   x = P6IN * 2;

the compiler won't mistake.

By the way, I think it's also better to change your initial example:

unsigned two;
void foo(void)
{
two = P6IN + P6IN;
}

into:

unsigned two;

void foo(void)
unsigned int tmp;
{
tmp = P6IN + P6IN;  //  <-----  tmp = 2 * P6IN;  //  <----- tmp =
P6IN; tmp+= P6IN;
two = tmp;
}

or even better a function. it's more efficient (and easier to compile)
if you make the calculations through local variables.


Roberto



2006/7/1, Grant Edwards <gra...@visi.com>:
On 2006-06-30, Chris Liechti <cliec...@gmx.net> wrote:

>>>>Just for fun, I tried compiling the code with "two" changed to
>>>>an unsigned char.  My mps430 compiler (3.2.3) then gives
>>>>
>>>>    mov.b &P6IN, r15
>>>>    rla.b r15
>>>>    mov.b r15, &two
>>>>    ret
>>>>
>>>>In other words, it makes the same mistake you did and
>>>>disregards the "volatile" qualifier.  This is far more serious
>>>>than the original question - it is incorrect code, rather than
>>>>just inefficient code.

>> Rather than just complaining, I should try to fix it.  Is this
>> something that could be fixed by somebody like me who's only
>> done minor hacking on GCC?
>
> if you already have an idea how the backend of gcc works i
> could think of "yes"

I've read descriptions of the backend, but never worked on it.
I've only tweaked a few things in the front end.

> gcc likes to output code for volatile acceess as
> load/modify/store, even when with the MSP430 a single mov,
> bis, bic/and would be fine too.  there are extra optinizations
> by mspgcc to generate efficent code in such a case. that might
> also explain the different code at the two optimization
> levels.

It does generate correct code for -O0:

foo:
        mov.b   &0x0034, r15
        add.b   &0x0034, r15
        mov.b   r15, &two
        ret

At -O1 and -O2, it generates the incorrect code as previously
noted.

> maybe someone could check with -mno-volatile-workaround

No difference.  It still generates incorrect code for the
-O[12] case when the destination matches the width of the
source (both 8 bits wide). For the original case where the
destination i s16 bits wide, it stell generates an extra mov.b
and an extraneous and.b #-1,r15.

>> I see that there are several more recent versions of mspgcc in
>> CVS.  Are any of the newer ones ready for production use?  If
>> not, is 3.2.3 still being maintained?
>
> 3.2.3 is our "stable" version

Is it getting bug fixes?

> the latest 4.x should be the next version, but it's not yet
> ready

--
Grant Edwards                   grante             Yow!  YOU'D cry too if it
                                  at               happened to YOU!!
                               visi.com


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Mspgcc-users mailing list
Mspgcc-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mspgcc-users


Reply via email to