Menu

#76 sdcc should not sort switch cases

None
pending
nobody
None
1
2016-04-13
2004-06-10
No

/* SDCC should not sort switch cases if it does not
optimize the switch statement
into a jump table
*/

#include <8052.h>

void f( unsigned char c )
{
volatile char t=c;
}

void main(void)
{
while (1) {
#pragma save
#pragma nojtbound
switch (P1 & 0x0c) {
case 0x0c: f(0); break; // this is the most
frequently taken path
// (internal
pullups, no external switch closed)
case 0x00: f(3); break;
case 0x04: f(2); break;
case 0x08: f(1); break;
}
#pragma restore
}
}

Discussion

  • Philipp Klaus Krause

    • status: open --> pending
    • Group: -->
     
  • Philipp Klaus Krause

    I don't see any justification for the assumption about the earlier switch cases being the more frequent ones.
    I'd suggest to close this.

    Philipp

     
  • Frieder Ferlemann

    The provided code snippet provides reasoning for the label 0x0c being used (much) more frequently than the others. (reasoning not assumption:)

    By contrast SDCC currently generates code on the assumption that the labels that sort lowest are those which should be tested first.
    SDCC's thus overrides the choice of the programmer.

     
  • Maarten Brock

    Maarten Brock - 2016-04-13

    I suggest to keep this request open. Of course C promises no priority, but there also is no reason to sort just because it can,

     

Log in to post a comment.