[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Generating code using sbis, sbic

From: David Brown
Subject: Re: [avr-gcc-list] Generating code using sbis, sbic
Date: Fri, 19 Aug 2005 09:33:24 +0200


Yes, I understand about integer promotion (although I have never been clear
on all the cases when it applies).  However, as far as I understand it,
there is nothing in the C standard that says you *actually* have to promote
to integer - just that the code should work as though it had been promoted.
If all the C code that handles the results of this function only look at the
low byte, then the high byte can be ignored.  I'm sure it's possible to
write some horrible code that actually relies on char -> int promotion
occuring in a function result, but I don't think it would be a problem for
most sane code.  A compiler flag to turn off integer promotion on the AVR
port would be a definite boost to code quality (on 16-bit and 32-bit
targets, however, it could well be a net loss - it would only help on a
target which can't work with integers properly).  I guess the other trick,
especially on a Tiny (using no library function), would be to compile
with -mint8.

Anyway, even with the integer promotion, the code is not optimal - is there
no peephole optimisation that would move the "ldi r25, hi8(1)" above the
test?  Better code would be:
        clr r25            // If ANSI insists...
        clr r24
        sbic 54-0x20,1
            inc r24

Using c++ like this is an interesting idea - I haven't tried c++ on the AVR
as yet.  I guess if you collect some useful little classes like this in a
single header (with the member functions inlined), it should be quite

But my real gripe is not so much the code generated for test2 (not optimal,
but not too bad), but with the code generated for test1, which is a complete



----- Original Message -----
From: "Haase Bjoern (PT-BEU/EMT) *" <address@hidden>
To: "David Brown" <address@hidden>; <address@hidden>
Sent: Thursday, August 18, 2005 7:17 PM
Subject: AW: [avr-gcc-list] Generating code using sbis, sbic

> David Brown wrote
But the generated code for test2 itself is:
        sbis 54-0x20,1
        rjmp .L9
        ldi r24, lo8(1)
        ldi r25, hi8(1)
        ldi r24, lo8(0)
        ldi r25, hi8(0)

Hardly optimal (does the result really have to be promoted to an
but not bad.  The generated code for test1 is extraordinary:

Unfortunately: This integer promotion is prescribed by the c standard.

If you are using c++ you could overcome it by defining a class uint8_c
and by
Specifying this type as return parameter:

#include <inttypes.h>

class uint8_c {

  uint8_c (uint8_t initval)
    value = initval;

  operator= (uint8_t initval)
    value = initval;
    return *this;

  operator uint8_t (void)
    return value;

  uint8_t value;

test (void)
  return 1;

Test (void)
  return 1;


.type _Z4testv, @function
ldi r24,lo8(1)

.size _Z4testv, .-_Z4testv
.global _Z5test2v
.type _Z5test2v, @function
ldi r24,lo8(1)
ldi r25,hi8(1)

AVR-GCC-list mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]