On 2006-07-03, Roberto Padovani <padovan...@gmail.com> wrote:

> but in this case, reading the C source code, I honestly can't
> understand for sure what it meant....

What it "meant" is irrelevent.  There are rules that state
quite specifically how C source code is to be translated into
machine operations.  For one of the cases under discussion the
compiler broke those rules.

> 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 ?

What the programmer wants is irrelevent.  The compiler is
required to do what the programmer _wrote_.

> IMHO the answer wasn't clear, even if I considered all the
> volatile qualifiers;

You're right, the answer isn't clear. But, the question you're
asking isn't material.  The compiler is required to generate
code from what the programmer wrote by following a specified
set of rules. What the programmer wanted or meant just doesn't
matter.

> so I wouldn't try to add another complicated rule to gcc,

Nobody's attempting to "add another compilcated rule to gcc.
The problem is that gcc isn't following an existing rule.  And
it _appears_ to be a bug specific to the MSP430 port.

> but I would rather write a better C code...an unambiguous one

The C code is unambiguous according the rules of the C
language.  Compilers have to generate correct code for valid
input -- regardless of how "good" or "bad" that input is.

> that the "simple" rules of any compiler can understand.

When writing a compiler, you're not supposed to change the
rules of C.  The input code was not ambiguous: it specified
that the variable P6IN was to be read exactly twice.

> for sampling twice:  x = P6IN;   x += P6IN;
> for doubling the sampling:   x = P6IN * 2;
>
> the compiler won't mistake.

If the compiler is free to break the rules, how do I know that
either of your two examples will generate correct code?

> 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.

I've no clue what you're talking about.  That doesn't even look
like valid C code to me.

-- 
Grant Edwards                   grante             Yow!  Is the EIGHTIES
                                  at               when they had ART DECO
                               visi.com            and GERALD McBOING-BOING
                                                   lunch boxes??


Reply via email to