#54 trinary operator generates unnecessary operations

closed-out-of-date
None
5
2007-06-09
2006-01-20
Hubert Sack
No

If the trinary operator is used and the destination is
a bit, a lot of unnecessary operations are generated:

#include <8051.h>

#define LED_OFF 1
#define LED_ON 0

#define LED_at_Portpin P1_0

unsigned char value = 0;

void main (void)
{
LED_at_Portpin = value < 2 ? LED_OFF : LED_ON;
}

The code doesn't become better if 0 and 1 is defined
as false and true using stdbool.h
In this case a additional sloc is generated.
I solved this by adding 3 more peepholes. I think the
solution can and should be made at icode level by
anybody...

Discussion

  • Hubert Sack

    Hubert Sack - 2006-01-20

    changes made in "mcs51/peeph.def"

     
  • Maarten Brock

    Maarten Brock - 2007-06-09
    • assigned_to: nobody --> maartenbrock
    • status: open --> closed-out-of-date
     
  • Maarten Brock

    Maarten Brock - 2007-06-09

    Logged In: YES
    user_id=888171
    Originator: NO

    SDCC 2.7.2 #4842 now generates the most compact code I can think of without peephole rules:

    mov a,#0x100 - 0x02
    add a,_value
    mov _pin,c

    or if value is a parameter or local variable and in a register:

    cjne r2,#0x02,00105$
    00105$:
    mov _pin,c

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks